]> Savannah Git Hosting - gnulib.git/commitdiff
getpass: Check for nonnull prompt argument while avoiding warnings.
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 13 Sep 2020 18:59:53 +0000 (20:59 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 13 Sep 2020 19:00:58 +0000 (21:00 +0200)
The prompt parameter to getpass() is declared as nonnull (using a GCC
nonnull attribute).  Gnulib contains two implementations of this function,
one for POSIX, one for Windows.  The Windows implementation checked for
a nonnull prompt, which caused a GCC warning.  This commit fixes that by
avoiding the nonnull attribute when building getpass.c.  The POSIX
implementation did not check for a nonnull prompt.  This commit increases
the robustness by adding such a check.

* lib/getpass.c (_GL_ARG_NONNULL): Define to empty.
(getpass) [!_WIN32]: Print prompt only if nonnull.

ChangeLog
lib/getpass.c

index 66aec61cb2cba0b315722135ec454c01bc0906aa..62e1f17d326555d6aafe85b57044ce227b206477 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2020-09-13  Ben Pfaff  <blp@cs.stanford.edu>
+
+       getpass: Check for nonnull prompt argument while avoiding warnings.
+       * lib/getpass.c (_GL_ARG_NONNULL): Define to empty.
+       (getpass) [!_WIN32]: Print prompt only if nonnull.
+
 2020-09-12  Paul Eggert  <eggert@cs.ucla.edu>
 
        dfa: epsilon-closure tweaks (Bug#40634)
index 3b0552ec58ce0a70e69c18938846eef74a40bf80..ca528fdc09acc7da6b3627abbaaf644bc3b91b26 100644 (file)
@@ -16,6 +16,9 @@
    with this program; if not, see <https://www.gnu.org/licenses/>.  */
 
 #ifndef _LIBC
+/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
+   warns for the null checks on 'prompt' below.  */
+# define _GL_ARG_NONNULL(params)
 # include <config.h>
 #endif
 
@@ -124,9 +127,12 @@ getpass (const char *prompt)
     }
 # endif
 
-  /* Write the prompt.  */
-  fputs_unlocked (prompt, out);
-  fflush_unlocked (out);
+  if (prompt)
+    {
+      /* Write the prompt.  */
+      fputs_unlocked (prompt, out);
+      fflush_unlocked (out);
+    }
 
   /* Read the password.  */
   nread = getline (&buf, &bufsize, in);