gnunet

Main GNUnet Logic
Log | Files | Refs | Submodules | README | LICENSE

commit 397d2a489f6af963ec63f8f5473469e46b7619f4
parent cc81eac80185a80edec6208fba6961cb392a53b8
Author: Christian Grothoff <christian@grothoff.org>
Date:   Sat, 14 Dec 2013 23:43:20 +0000

-fixing leaks on error paths

Diffstat:
Msrc/util/program.c | 80+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Msrc/util/speedup.c | 18++++++++++++------
2 files changed, 58 insertions(+), 40 deletions(-)

diff --git a/src/util/program.c b/src/util/program.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2009 Christian Grothoff (and other contributing authors) + (C) 2009-2013 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -55,7 +55,7 @@ struct CommandContext GNUNET_PROGRAM_Main task; /** - * Closure for task. + * Closure for @e task. */ void *task_cls; @@ -66,9 +66,20 @@ struct CommandContext }; + +/** + * Start task that may speed up our system clock artificially + * + * @param cfg configuration to use + * @return #GNUNET_OK on success, #GNUNET_SYSERR if the speedup was not configured + */ int GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg); + +/** + * Stop tasks that modify clock behavior. + */ int GNUNET_SPEEDUP_stop_ (void); @@ -121,16 +132,16 @@ cmd_sorter (const void *a1, const void *a2) * Run a standard GNUnet command startup sequence (initialize loggers * and configuration, parse options). * - * @param argc number of command line arguments + * @param argc number of command line arguments in @a argv * @param argv command line arguments * @param binaryName our expected name * @param binaryHelp help text for the program * @param options command line options * @param task main function to run - * @param task_cls closure for task - * @param run_without_scheduler GNUNET_NO start the scheduler, GNUNET_YES do not + * @param task_cls closure for @a task + * @param run_without_scheduler #GNUNET_NO start the scheduler, #GNUNET_YES do not * start the scheduler just run the main task - * @return GNUNET_SYSERR on error, GNUNET_OK on success + * @return #GNUNET_SYSERR on error, #GNUNET_OK on success */ int GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName, @@ -168,7 +179,7 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName, logfile = NULL; gargs = getenv ("GNUNET_ARGS"); - if (gargs != NULL) + if (NULL != gargs) { char **gargv; unsigned int gargc; @@ -181,12 +192,8 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName, for (i = 0; i < argc; i++) GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i])); cargs = GNUNET_strdup (gargs); - tok = strtok (cargs, " "); - while (NULL != tok) - { + for (tok = strtok (cargs, " "); NULL != tok; tok = strtok (NULL, " ")) GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok)); - tok = strtok (NULL, " "); - } GNUNET_free (cargs); GNUNET_array_append (gargv, gargc, NULL); argv = (char *const *) gargv; @@ -201,7 +208,7 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName, #if ENABLE_NLS setlocale (LC_ALL, ""); path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR); - if (path != NULL) + if (NULL != path) { BINDTEXTDOMAIN ("GNUnet", path); GNUNET_free (path); @@ -209,7 +216,7 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName, textdomain ("GNUnet"); #endif cnt = 0; - while (options[cnt].name != NULL) + while (NULL != options[cnt].name) cnt++; allopts = GNUNET_malloc ((cnt + @@ -240,14 +247,9 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName, if ((GNUNET_OK > ret) || (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile))) { - GNUNET_CONFIGURATION_destroy (cfg); - GNUNET_free_non_null (cc.cfgfile); - GNUNET_free_non_null (loglev); - GNUNET_free_non_null (logfile); - GNUNET_free (cfg_fn); GNUNET_free (allopts); GNUNET_free (lpfx); - return (ret == GNUNET_SYSERR) ? GNUNET_SYSERR : GNUNET_OK; + goto cleanup; } if (NULL == cc.cfgfile) cc.cfgfile = GNUNET_strdup (cfg_fn); @@ -259,8 +261,10 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName, GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Malformed configuration file `%s', exit ...\n"), cc.cfgfile); - GNUNET_free (cc.cfgfile); - return GNUNET_SYSERR; + ret = GNUNET_SYSERR; + GNUNET_free (allopts); + GNUNET_free (lpfx); + goto cleanup; } } else @@ -273,8 +277,10 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName, { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Malformed configuration, exit ...\n")); - GNUNET_free (cc.cfgfile); - return GNUNET_SYSERR; + ret = GNUNET_SYSERR; + GNUNET_free (allopts); + GNUNET_free (lpfx); + goto cleanup; } } GNUNET_free (allopts); @@ -293,21 +299,22 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName, cc.args = &argv[ret]; if (GNUNET_NO == run_without_scheduler) { - GNUNET_SCHEDULER_run (&program_main, &cc); + GNUNET_SCHEDULER_run (&program_main, &cc); } else { - GNUNET_RESOLVER_connect (cc.cfg); - cc.task (cc.task_cls, cc.args, cc.cfgfile, cc.cfg); + GNUNET_RESOLVER_connect (cc.cfg); + cc.task (cc.task_cls, cc.args, cc.cfgfile, cc.cfg); } - /* clean up */ + ret = GNUNET_OK; + cleanup: GNUNET_SPEEDUP_stop_ (); GNUNET_CONFIGURATION_destroy (cfg); - GNUNET_free (cc.cfgfile); + GNUNET_free_non_null (cc.cfgfile); GNUNET_free (cfg_fn); GNUNET_free_non_null (loglev); GNUNET_free_non_null (logfile); - return GNUNET_OK; + return ret; } /** @@ -324,14 +331,19 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName, * @return #GNUNET_SYSERR on error, #GNUNET_OK on success */ int -GNUNET_PROGRAM_run (int argc, char *const *argv, const char *binaryName, +GNUNET_PROGRAM_run (int argc, char *const *argv, + const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, - GNUNET_PROGRAM_Main task, void *task_cls) + GNUNET_PROGRAM_Main task, + void *task_cls) { - return GNUNET_PROGRAM_run2 (argc, argv, binaryName, binaryHelp, options, task, task_cls, GNUNET_NO); + return GNUNET_PROGRAM_run2 (argc, argv, + binaryName, binaryHelp, + options, + task, task_cls, + GNUNET_NO); } - /* end of program.c */ diff --git a/src/util/speedup.c b/src/util/speedup.c @@ -37,7 +37,8 @@ static GNUNET_SCHEDULER_TaskIdentifier speedup_task; static void -do_speedup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +do_speedup (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { static long long current_offset; @@ -57,14 +58,18 @@ do_speedup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) * Start task that may speed up our system clock artificially * * @param cfg configuration to use - * @return GNUNET_OK on success, GNUNET_SYSERR if the speedup was not configured + * @return #GNUNET_OK on success, #GNUNET_SYSERR if the speedup was not configured */ int GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg) { - if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "testing", "SPEEDUP_INTERVAL", &interval)) + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_time (cfg, "testing", + "SPEEDUP_INTERVAL", &interval)) return GNUNET_SYSERR; - if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "testing", "SPEEDUP_DELTA", &delta)) + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_time (cfg, "testing", + "SPEEDUP_DELTA", &delta)) return GNUNET_SYSERR; if ((0 == interval.rel_value_us) || (0 == delta.rel_value_us)) @@ -79,7 +84,8 @@ GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg) LOG (GNUNET_ERROR_TYPE_DEBUG, "Speed up executed every %s\n", GNUNET_STRINGS_relative_time_to_string (interval, GNUNET_NO)); - speedup_task = GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO, &do_speedup, NULL); + speedup_task = GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO, + &do_speedup, NULL); return GNUNET_OK; } @@ -88,7 +94,7 @@ GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg) * Stop tasks that modify clock behavior. */ void -GNUNET_SPEEDUP_stop_ ( ) +GNUNET_SPEEDUP_stop_ () { if (GNUNET_SCHEDULER_NO_TASK != speedup_task) {