From 73548f6be3e2d463a1ffeb68d2c10bb37381d7ba Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 4 May 2009 15:50:06 -0300 Subject: [PATCH] all: Fix possible uninitialized variable uses I wasn't especifying the optimization level and the default, despite using -Wall, was for this so simple case not to be warned about, so now I'm using -O2. Alexandre provided a patch initializing the variables to NULL, so that when we called cus__delete it would bail out and not possibly act on a random value, I preferred to add extra goto labels and do the exit path only on the resources that were successfully allocated/initialized, avoiding, for instance, to call dwarves_exit() if dwarves_init() wasn't called, which wasn't a problem so far, but could be in the future. Reported-by: Alexandre Vassalotti Signed-off-by: Arnaldo Carvalho de Melo --- codiff.c | 21 +++++++++++---------- dwarves_reorganize.c | 2 +- pahole.c | 16 +++++++++++----- pfunct.c | 16 +++++++++++----- pglobal.c | 16 +++++++++++----- 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/codiff.c b/codiff.c index 9a4a680..ed8d9a2 100644 --- a/codiff.c +++ b/codiff.c @@ -733,7 +733,6 @@ static struct argp codiff__argp = { int main(int argc, char *argv[]) { int remaining, err, rc = EXIT_FAILURE; - struct cus *old_cus, *new_cus; char *old_filename, *new_filename; char *filenames[2]; struct stat st; @@ -754,7 +753,7 @@ failure: if (dwarves__init(0)) { fputs("codiff: insufficient memory\n", stderr); - return EXIT_FAILURE; + goto out; } if (show_function_diffs == 0 && show_struct_diffs == 0 && @@ -762,16 +761,16 @@ failure: show_function_diffs = show_struct_diffs = 1; structs_printed = strlist__new(false); - old_cus = cus__new(); - new_cus = cus__new(); + struct cus *old_cus = cus__new(), + *new_cus = cus__new(); if (old_cus == NULL || new_cus == NULL || structs_printed == NULL) { fputs("codiff: insufficient memory\n", stderr); - return EXIT_FAILURE; + goto out_cus_delete; } if (stat(old_filename, &st) != 0) { fprintf(stderr, "codiff: %s (%s)\n", strerror(errno), old_filename); - return EXIT_FAILURE; + goto out_cus_delete; } filenames[1] = NULL; @@ -781,13 +780,13 @@ failure: err = cus__load_file(old_cus, &conf_load, old_filename); if (err != 0) { cus__print_error_msg("codiff", old_cus, old_filename, err); - return EXIT_FAILURE; + goto out_cus_delete_priv; } } if (stat(new_filename, &st) != 0) { fprintf(stderr, "codiff: %s (%s)\n", strerror(errno), new_filename); - return EXIT_FAILURE; + goto out_cus_delete_priv; } /* If old_file is a character device, leave its cus empty */ @@ -795,7 +794,7 @@ failure: err = cus__load_file(new_cus, &conf_load, new_filename); if (err != 0) { cus__print_error_msg("codiff", new_cus, new_filename, err); - return EXIT_FAILURE; + goto out_cus_delete_priv; } } @@ -810,12 +809,14 @@ failure: } rc = EXIT_SUCCESS; -out: +out_cus_delete_priv: cus__for_each_cu(old_cus, cu_delete_priv, NULL, NULL); cus__for_each_cu(new_cus, cu_delete_priv, NULL, NULL); +out_cus_delete: cus__delete(old_cus); cus__delete(new_cus); strlist__delete(structs_printed); dwarves__exit(); +out: return rc; } diff --git a/dwarves_reorganize.c b/dwarves_reorganize.c index 18ff807..9fa8cfa 100644 --- a/dwarves_reorganize.c +++ b/dwarves_reorganize.c @@ -245,7 +245,7 @@ static void class__move_member(struct class *class, struct class_member *dest, list_prepare_entry(from, class__tags(class), tag.node); struct class_member *tmp; - uint8_t orig_tail_from_bit_hole; + uint8_t orig_tail_from_bit_hole = 0; LIST_HEAD(from_list); if (verbose) diff --git a/pahole.c b/pahole.c index 76fa686..0ba7dde 100644 --- a/pahole.c +++ b/pahole.c @@ -1118,7 +1118,6 @@ dump_it: int main(int argc, char *argv[]) { int err, remaining, rc = EXIT_FAILURE; - struct cus *cus; if (argp_parse(&pahole__argp, argc, argv, 0, &remaining, NULL) || remaining == argc) { @@ -1126,26 +1125,33 @@ int main(int argc, char *argv[]) goto out; } - cus = cus__new(); - if (dwarves__init(cacheline_size) || cus == NULL) { + if (dwarves__init(cacheline_size)) { fputs("pahole: insufficient memory\n", stderr); goto out; } + struct cus *cus = cus__new(); + if (cus == NULL) { + fputs("pahole: insufficient memory\n", stderr); + goto out_dwarves_exit; + } + conf_load.steal = pahole_stealer; err = cus__load_files(cus, &conf_load, argv + remaining); if (err != 0) { fputs("pahole: No debugging information found\n", stderr); - goto out; + goto out_cus_delete; } if (stats_formatter != NULL) print_stats(); rc = EXIT_SUCCESS; -out: +out_cus_delete: cus__delete(cus); structures__delete(); +out_dwarves_exit: dwarves__exit(); +out: return rc; } diff --git a/pfunct.c b/pfunct.c index a721081..52b6ada 100644 --- a/pfunct.c +++ b/pfunct.c @@ -613,7 +613,6 @@ static struct argp pfunct__argp = { int main(int argc, char *argv[]) { int err, remaining, rc = EXIT_FAILURE; - struct cus *cus; if (argp_parse(&pfunct__argp, argc, argv, 0, &remaining, NULL) || remaining == argc) { @@ -624,15 +623,20 @@ int main(int argc, char *argv[]) if (symtab_name != NULL) return elf_symtabs__show(argv + remaining); - cus = cus__new(); - if (dwarves__init(0) || cus == NULL) { + if (dwarves__init(0)) { fputs("pfunct: insufficient memory\n", stderr); goto out; } + struct cus *cus = cus__new(); + if (cus == NULL) { + fputs("pfunct: insufficient memory\n", stderr); + goto out_dwarves_exit; + } + err = cus__load_files(cus, &conf_load, argv + remaining); if (err != 0) - goto out; + goto out_cus_delete; cus__for_each_cu(cus, cu_unique_iterator, NULL, NULL); @@ -647,9 +651,11 @@ int main(int argc, char *argv[]) print_fn_stats(formatter); rc = EXIT_SUCCESS; -out: +out_cus_delete: cus__delete(cus); fn_stats__delete_list(); +out_dwarves_exit: dwarves__exit(); +out: return rc; } diff --git a/pglobal.c b/pglobal.c index e8206fe..85c76e5 100644 --- a/pglobal.c +++ b/pglobal.c @@ -290,16 +290,20 @@ int main(int argc, char *argv[]) goto out; } - struct cus *cus = cus__new(); - - if (dwarves__init(0) || cus == NULL) { + if (dwarves__init(0)) { fputs("pglobal: insufficient memory\n", stderr); goto out; } + struct cus *cus = cus__new(); + if (cus == NULL) { + fputs("pglobal: insufficient memory\n", stderr); + goto out_dwarves_exit; + } + err = cus__load_files(cus, NULL, argv + remaining); if (err != 0) - goto out; + goto out_cus_delete; if (walk_var) { cus__for_each_cu(cus, cu_extvar_iterator, NULL, NULL); @@ -311,8 +315,10 @@ int main(int argc, char *argv[]) tdestroy(tree, free_node); rc = EXIT_SUCCESS; -out: +out_cus_delete: cus__delete(cus); +out_dwarves_exit: dwarves__exit(); +out: return rc; }