From 68d9288da5d67fb6bbace6087df79136a5813c2a Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 5 Sep 2023 00:22:30 +0200 Subject: [PATCH] argp: Fix gcc -Wanalyzer-use-of-uninitialized-value warning. * lib/argp-help.c (hol_find_entry): Access hol->entries only after having verified that hol->num_entries > 0. --- ChangeLog | 4 ++++ lib/argp-help.c | 29 +++++++++++++++++------------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6690e0c367..a574534213 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2023-09-04 Bruno Haible + argp: Fix gcc -Wanalyzer-use-of-uninitialized-value warning. + * lib/argp-help.c (hol_find_entry): Access hol->entries only after + having verified that hol->num_entries > 0. + unigbrk/uc-grapheme-breaks tests: Fix gcc -Wunused-function warning. * tests/unigbrk/test-uc-grapheme-breaks.c (graphemebreakproperty_to_string): Mark as possibly unused. diff --git a/lib/argp-help.c b/lib/argp-help.c index e5baee2ca8..7a277ebf13 100644 --- a/lib/argp-help.c +++ b/lib/argp-help.c @@ -422,8 +422,8 @@ struct hol { /* An array of hol_entry's. */ struct hol_entry *entries; - /* The number of entries in this hol. If this field is zero, the others - are undefined. */ + /* The number of entries in this hol. If this field is zero, entries and + short_options are undefined. */ unsigned num_entries; /* A string containing all short options in this HOL. Each entry contains @@ -642,21 +642,26 @@ hol_entry_first_long (const struct hol_entry *entry) static struct hol_entry * hol_find_entry (struct hol *hol, const char *name) { - struct hol_entry *entry = hol->entries; unsigned num_entries = hol->num_entries; - while (num_entries-- > 0) + if (num_entries > 0) { - const struct argp_option *opt = entry->opt; - unsigned num_opts = entry->num; + struct hol_entry *entry = hol->entries; - while (num_opts-- > 0) - if (opt->name && ovisible (opt) && strcmp (opt->name, name) == 0) - return entry; - else - opt++; + do + { + const struct argp_option *opt = entry->opt; + unsigned num_opts = entry->num; - entry++; + while (num_opts-- > 0) + if (opt->name && ovisible (opt) && strcmp (opt->name, name) == 0) + return entry; + else + opt++; + + entry++; + } + while (--num_entries > 0); } return 0; -- 2.39.5