diff mbox series

[v2] error: Don't call va_end twice [BZ #32233].

Message ID 20241004021920.12796-1-collin.funk1@gmail.com
State New
Headers show
Series [v2] error: Don't call va_end twice [BZ #32233]. | expand

Commit Message

Collin Funk Oct. 4, 2024, 2:19 a.m. UTC
Hi Sam,

Makes sense, was a bit lazy of me. This should be better.

-- 8< --

Sync to Gnulib commit ba9136b7646ef9e01ffad3ddcf232002354531d0

* misc/error.c (error_tail): Remove va_end call.
(__error_internal): Use the '%u' conversion specifier instead of '%d'
for 'line_number' which is an unsigned int.

The following changes are only applicable to Gnulib:

* misc/error.c: Use #include <error.h> instead of #include "error.h" to
silence Clang's -Winclude-next-absolute-path.
(__error_internal, __error_at_line_internal, error_tail): Define to
Gnulib symbols.
[_WIN32 && !__CYGWIN__]: Include io.h for Windows function declarations.
(strerror_r): Use a C23 compatible declaration.
(program_name): Define to getprogname.
(verror, verror_at_line) [GNULIB_defined_verror]: Define symbols.

Signed-off-by: Collin Funk <collin.funk1@gmail.com>
---
 misc/error.c | 89 ++++++++++++++++++++++++++++++----------------------
 1 file changed, 52 insertions(+), 37 deletions(-)
diff mbox series

Patch

diff --git a/misc/error.c b/misc/error.c
index e5d5588434..6816805326 100644
--- a/misc/error.c
+++ b/misc/error.c
@@ -20,9 +20,16 @@ 
 
 #if !_LIBC
 # include <config.h>
+# define _GL_NO_INLINE_ERROR
+# define __error_internal(status, err, fmt, args, flags) \
+                  verror (status, err, fmt, args)
+# define __error_at_line_internal(status, err, file, line, fmt, args, flags) \
+                  verror_at_line (status, err, file, line, fmt, args)
+# define error_tail(status, err, fmt, args, flags) \
+        error_tail (status, err, fmt, args)
 #endif
 
-#include "error.h"
+#include <error.h>
 
 #include <stdarg.h>
 #include <stdio.h>
@@ -41,7 +48,7 @@ 
 # include <wchar.h>
 # define mbsrtowcs __mbsrtowcs
 # define USE_UNLOCKED_IO 0
-# define _GL_ATTRIBUTE_FORMAT_PRINTF(a, b)
+# define _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD(a, b)
 # define _GL_ARG_NONNULL(a)
 #endif
 
@@ -74,9 +81,9 @@  unsigned int error_message_count;
 extern void __error (int status, int errnum, const char *message, ...)
      __attribute__ ((__format__ (__printf__, 3, 4)));
 extern void __error_at_line (int status, int errnum, const char *file_name,
-			     unsigned int line_number, const char *message,
-			     ...)
-     __attribute__ ((__format__ (__printf__, 5, 6)));;
+                             unsigned int line_number, const char *message,
+                             ...)
+     __attribute__ ((__format__ (__printf__, 5, 6)));
 # define error __error
 # define error_at_line __error_at_line
 
@@ -92,43 +99,52 @@  extern void __error_at_line (int status, int errnum, const char *file_name,
 # include <fcntl.h>
 # include <unistd.h>
 
-# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+# if defined _WIN32 && ! defined __CYGWIN__
 /* Get declarations of the native Windows API functions.  */
 #  define WIN32_LEAN_AND_MEAN
 #  include <windows.h>
 /* Get _get_osfhandle.  */
-#  include "msvc-nothrow.h"
+#  if GNULIB_MSVC_NOTHROW
+#   include "msvc-nothrow.h"
+#  else
+#   include <io.h>
+#  endif
 # endif
 
 /* The gnulib override of fcntl is not needed in this file.  */
 # undef fcntl
 
-# if !HAVE_DECL_STRERROR_R
+# if !(GNULIB_STRERROR_R_POSIX || HAVE_DECL_STRERROR_R)
 #  ifndef HAVE_DECL_STRERROR_R
 "this configure-time declaration test was not run"
 #  endif
 #  if STRERROR_R_CHAR_P
-char *strerror_r ();
+char *strerror_r (int errnum, char *buf, size_t buflen);
 #  else
-int strerror_r ();
+int strerror_r (int errnum, char *buf, size_t buflen);
 #  endif
 # endif
 
-/* The calling program should define program_name and set it to the
-   name of the executing program.  */
-extern char *program_name;
+# define program_name getprogname ()
 
-# if HAVE_STRERROR_R || defined strerror_r
+# if GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r
 #  define __strerror_r strerror_r
-# endif	/* HAVE_STRERROR_R || defined strerror_r */
-#endif	/* not _LIBC */
+# endif /* GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r */
+
+# if GNULIB_defined_verror
+#  undef verror
+# endif
+# if GNULIB_defined_verror_at_line
+#  undef verror_at_line
+# endif
+#endif  /* not _LIBC */
 
 #if !_LIBC
 /* Return non-zero if FD is open.  */
 static int
 is_open (int fd)
 {
-# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+# if defined _WIN32 && ! defined __CYGWIN__
   /* On native Windows: The initial state of unassigned standard file
      descriptors is that they are open but point to an INVALID_HANDLE_VALUE.
      There is no fcntl, and the gnulib replacement fcntl does not support
@@ -173,9 +189,9 @@  print_errno_message (int errnum)
 {
   char const *s;
 
-#if defined HAVE_STRERROR_R || _LIBC
+#if _LIBC || GNULIB_STRERROR_R_POSIX || defined HAVE_STRERROR_R
   char errbuf[1024];
-# if _LIBC || STRERROR_R_CHAR_P
+# if _LIBC || (!GNULIB_STRERROR_R_POSIX && STRERROR_R_CHAR_P)
   s = __strerror_r (errnum, errbuf, sizeof errbuf);
 # else
   if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
@@ -199,9 +215,9 @@  print_errno_message (int errnum)
 #endif
 }
 
-static void _GL_ATTRIBUTE_FORMAT_PRINTF (3, 0) _GL_ARG_NONNULL ((3))
+static void _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (3, 0) _GL_ARG_NONNULL ((3))
 error_tail (int status, int errnum, const char *message, va_list args,
-	    unsigned int mode_flags)
+            unsigned int mode_flags)
 {
 #if _LIBC
   int ret = __vfxprintf (stderr, message, args, mode_flags);
@@ -212,7 +228,6 @@  error_tail (int status, int errnum, const char *message, va_list args,
 #else
   vfprintf (stderr, message, args);
 #endif
-  va_end (args);
 
   ++error_message_count;
   if (errnum)
@@ -234,7 +249,7 @@  error_tail (int status, int errnum, const char *message, va_list args,
    Exit with status STATUS if it is nonzero.  */
 void
 __error_internal (int status, int errnum, const char *message,
-		  va_list args, unsigned int mode_flags)
+                  va_list args, unsigned int mode_flags)
 {
 #if defined _LIBC
   /* We do not want this call to be cut short by a thread
@@ -281,8 +296,8 @@  int error_one_per_line;
 
 void
 __error_at_line_internal (int status, int errnum, const char *file_name,
-			  unsigned int line_number, const char *message,
-			  va_list args, unsigned int mode_flags)
+                          unsigned int line_number, const char *message,
+                          va_list args, unsigned int mode_flags)
 {
   if (error_one_per_line)
     {
@@ -290,13 +305,13 @@  __error_at_line_internal (int status, int errnum, const char *file_name,
       static unsigned int old_line_number;
 
       if (old_line_number == line_number
-	  && (file_name == old_file_name
-	      || (old_file_name != NULL
-		  && file_name != NULL
-		  && strcmp (old_file_name, file_name) == 0)))
+          && (file_name == old_file_name
+              || (old_file_name != NULL
+                  && file_name != NULL
+                  && strcmp (old_file_name, file_name) == 0)))
 
-	/* Simply return and print nothing.  */
-	return;
+        /* Simply return and print nothing.  */
+        return;
 
       old_file_name = file_name;
       old_line_number = line_number;
@@ -325,11 +340,11 @@  __error_at_line_internal (int status, int errnum, const char *file_name,
     }
 
 #if _LIBC
-  __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
-	      file_name, line_number);
+  __fxprintf (NULL, file_name != NULL ? "%s:%u: " : " ",
+              file_name, line_number);
 #else
-  fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
-	   file_name, line_number);
+  fprintf (stderr, file_name != NULL ? "%s:%u: " : " ",
+           file_name, line_number);
 #endif
 
   error_tail (status, errnum, message, args, mode_flags);
@@ -342,12 +357,12 @@  __error_at_line_internal (int status, int errnum, const char *file_name,
 
 void
 error_at_line (int status, int errnum, const char *file_name,
-	       unsigned int line_number, const char *message, ...)
+               unsigned int line_number, const char *message, ...)
 {
   va_list ap;
   va_start (ap, message);
   __error_at_line_internal (status, errnum, file_name, line_number,
-			    message, ap, 0);
+                            message, ap, 0);
   va_end (ap);
 }