bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

argp: Fix gcc -Wanalyzer-use-of-uninitialized-value warning


From: Bruno Haible
Subject: argp: Fix gcc -Wanalyzer-use-of-uninitialized-value warning
Date: Tue, 05 Sep 2023 00:23:57 +0200

This warning

argp-help.c:645:21: warning: use of uninitialized value '*hol.entries' 
[CWE-457] [-Wanalyzer-use-of-uninitialized-value]

is justified. Here's a fix.


2023-09-04  Bruno Haible  <bruno@clisp.org>

        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.

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;






reply via email to

[Prev in Thread] Current Thread [Next in Thread]