bug-gnulib
[Top][All Lists]
Advanced

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

getopt.c warnings patch


From: Paul J. Lucas
Subject: getopt.c warnings patch
Date: Wed, 27 May 2020 15:09:03 -0700

The getopt.c file generates the following warnings from Appleā€™s gcc (Apple 
clang version 11.0.3 (clang-1103.0.32.62)):

--------------------------------------------------------------------------------
getopt.c:208:21: warning: implicit conversion changes signedness: 'long' to
      'size_t' (aka 'unsigned long') [-Wsign-conversion]
  namelen = nameend - d->__nextchar;
          ~ ~~~~~~~~^~~~~~~~~~~~~~~
getopt.c:255:34: warning: implicit conversion changes signedness: 'int' to
      'unsigned long' [-Wsign-conversion]
                        else if ((ambig_set = malloc (n_options)) == NULL)
                                              ~~~~~~  ^~~~~~~~~
getopt.c:369:16: warning: variable 'option_index' may be uninitialized when used
      here [-Wconditional-uninitialized]
    *longind = option_index;
               ^~~~~~~~~~~~
getopt.c:204:19: note: initialize the variable 'option_index' to silence this
      warning
  int option_index;
                  ^
                   = 0
3 warnings generated.
--------------------------------------------------------------------------------

when compiled with these warnings enabled:

-Wall -Wcast-align -Wcomma -Wconditional-type-mismatch 
-Wconditional-uninitialized -Wconversion -Wextra -Wfloat-equal 
-Wfor-loop-analysis -Widiomatic-parentheses -Wimplicit-fallthrough 
-Wlogical-op-parentheses -Wnewline-eof -Wno-unknown-warning-option 
-Wredundant-decls -Wshadow -Wshift-sign-overflow -Wsign-compare 
-Wsign-conversion -Wsometimes-uninitialized -Wstring-conversion -Wuninitialized 
-Wunreachable-code-break -Wunreachable-code -Wunused -Wwrite-strings

Below is a patch that fixes all these warnings.

- Paul


--- lib/getopt.c.ORIG   2020-05-27 14:45:22.000000000 -0700
+++ lib/getopt.c        2020-05-27 14:57:42.000000000 -0700
@@ -201,11 +201,11 @@
   const struct option *p;
   const struct option *pfound = NULL;
   int n_options;
-  int option_index;
+  int option_index = 0;
 
   for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
     /* Do nothing.  */ ;
-  namelen = nameend - d->__nextchar;
+  namelen = (size_t)(nameend - d->__nextchar);
 
   /* First look for an exact match, counting the options as a side
      effect.  */
@@ -252,7 +252,7 @@
                      {
                        if (__libc_use_alloca (n_options))
                          ambig_set = alloca (n_options);
-                       else if ((ambig_set = malloc (n_options)) == NULL)
+                       else if ((ambig_set = malloc ((size_t)n_options)) == 
NULL)
                          /* Fall back to simpler error message.  */
                          ambig_fallback = 1;
                        else




reply via email to

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