bug-gnulib
[Top][All Lists]
Advanced

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

Re: getopt.c warnings patch


From: Bruno Haible
Subject: Re: getopt.c warnings patch
Date: Thu, 28 May 2020 01:59:09 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-177-generic; KDE/5.18.0; x86_64; ; )

Hi,

Paul J. Lucas wrote:
> 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)
>                                               ~~~~~~  ^~~~~~~~~

The obvious "fix" for these warnings is to introduce a cast. But such casts
would decrease the robustness of the code. As I wrote in [1], such explicit
casts introduce bugs when the standards change or some platform is not 100%
standards compliant.

Therefore it is best to ignore warnings of this type. That's what gnulib does,
through the file build-aux/gcc-warning.spec, when you use the
gl_MANYWARN_ALL_GCC macro.

> 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

Here the code is copying an uninitialized value, if pfound == NULL. But this is
harmless, because
  1) The documentation of _getopt_internal_r says that
       "LONGIND returns the index in LONGOPT of the long-named option found.
        It is only valid when a long-named option has been found by the most
        recent call."
  2) valgrind does not complain about copying an uninitialized value, if it ends
     up being unused.

Bruno

[1] https://bugs.llvm.org/show_bug.cgi?id=46025




reply via email to

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