bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: "Paul J. Lucas" <paul@lucasmail.org>
To: bug-gnulib@gnu.org
Subject: getopt.c warnings patch
Date: Wed, 27 May 2020 15:09:03 -0700	[thread overview]
Message-ID: <72E67607-48AF-4610-8A11-B16F2E2B9865@lucasmail.org> (raw)

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	other threads:[~2020-05-27 22:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-27 22:09 Paul J. Lucas [this message]
2020-05-27 23:59 ` getopt.c warnings patch Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=72E67607-48AF-4610-8A11-B16F2E2B9865@lucasmail.org \
    --to=paul@lucasmail.org \
    --cc=bug-gnulib@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).