diff options
author | Lucas De Marchi <lucas.demarchi@intel.com> | 2015-02-25 12:06:44 -0300 |
---|---|---|
committer | Lucas De Marchi <lucas.demarchi@intel.com> | 2015-02-25 12:06:44 -0300 |
commit | a07ea0329ca9655531f624b46719984da4604fbc (patch) | |
tree | ff5ff0838076d8c801816b8fea2977ccce865b46 /tools/depmod.c | |
parent | 0b3aef23b8e5cc935caaec6230782553c1a4ca95 (diff) | |
download | kmod-a07ea0329ca9655531f624b46719984da4604fbc.tar.gz kmod-a07ea0329ca9655531f624b46719984da4604fbc.tar.bz2 kmod-a07ea0329ca9655531f624b46719984da4604fbc.zip |
depmod: use cleanup attribute to simplify free on exit
Reusing the root variable was a bad idea. Doing so we could call free()
on a variable that was not allocated. For example: "depmod -b / -h".
Since we would jump to cmdline_failed, root would not be duplicated.
Instead of fighting the order in the options, just used the cleanup
attribute and remove the calls to free() on "config_paths" and "root".
Diffstat (limited to 'tools/depmod.c')
-rw-r--r-- | tools/depmod.c | 17 |
1 files changed, 5 insertions, 12 deletions
diff --git a/tools/depmod.c b/tools/depmod.c index 18aab5d..afde322 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -2378,8 +2378,8 @@ static int do_depmod(int argc, char *argv[]) { FILE *out = NULL; int err = 0, all = 0, maybe_all = 0, n_config_paths = 0; - char *root = NULL; - const char **config_paths = NULL; + _cleanup_free_ char *root = NULL; + _cleanup_free_ const char **config_paths = NULL; const char *system_map = NULL; const char *module_symvers = NULL; const char *null_kmod_config = NULL; @@ -2404,7 +2404,9 @@ static int do_depmod(int argc, char *argv[]) maybe_all = 1; break; case 'b': - root = optarg; + if (root) + free(root); + root = path_make_absolute_cwd(optarg); break; case 'C': { size_t bytes = sizeof(char *) * (n_config_paths + 2); @@ -2458,11 +2460,9 @@ static int do_depmod(int argc, char *argv[]) break; case 'h': help(); - free(config_paths); return EXIT_SUCCESS; case 'V': puts(PACKAGE " version " VERSION); - free(config_paths); return EXIT_SUCCESS; case '?': goto cmdline_failed; @@ -2483,9 +2483,6 @@ static int do_depmod(int argc, char *argv[]) cfg.kversion = un.release; } - if (root) - root = path_make_absolute_cwd(root); - cfg.dirnamelen = snprintf(cfg.dirname, PATH_MAX, "%s/lib/modules/%s", root == NULL ? "" : root, cfg.kversion); @@ -2596,8 +2593,6 @@ static int do_depmod(int argc, char *argv[]) done: depmod_shutdown(&depmod); cfg_free(&cfg); - free(config_paths); - free(root); return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE; cmdline_modules_failed: @@ -2607,8 +2602,6 @@ depmod_init_failed: kmod_unref(ctx); cmdline_failed: cfg_free(&cfg); - free(config_paths); - free(root); return EXIT_FAILURE; } |