]> Savannah Git Hosting - gnulib.git/commitdiff
error: don’t evaluate status arg twice
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 30 May 2023 19:49:20 +0000 (12:49 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 30 May 2023 21:39:25 +0000 (14:39 -0700)
This avoids potential issues if the first argument has a side effect.
* lib/error.in.h (__gl_error_call): New macro, which evaluates its
status arg only once, by using a statement expression if GNU C -
the only platform we need to worry about pacifying - and by simply
calling ‘error’ otherwise.
(error, error_at_line): Use it.

ChangeLog
lib/error.in.h

index 91a6135f8a4b0c4b683c32cfab92c86f0f78879f..f70eeef893cb383d0098982111f30d46be97abc5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2023-05-30  Paul Eggert  <eggert@cs.ucla.edu>
+
+       error: don’t evaluate status arg twice
+       This avoids potential issues if the first argument has a side effect.
+       * lib/error.in.h (__gl_error_call): New macro, which evaluates its
+       status arg only once, by using a statement expression if GNU C -
+       the only platform we need to worry about pacifying - and by simply
+       calling ‘error’ otherwise.
+       (error, error_at_line): Use it.
+
 2023-05-28  Bruno Haible  <bruno@clisp.org>
 
        warnings, manywarnings: Assume autoconf >= 2.64.
index 70fb1321330ce1826e938a62666d33d35540f26d..a5f49e41437a0f2d82aeaa5a888020afbcda58dc 100644 (file)
 # define _GL_ATTRIBUTE_SPEC_PRINTF_ERROR _GL_ATTRIBUTE_SPEC_PRINTF_SYSTEM
 #endif
 
+#ifdef __GNUC__
+# define __gl_error_call(function, status, ...) \
+    ({ \
+       int const __errstatus = status; \
+       (function) (0, __VA_ARGS__); \
+       __errstatus != 0 ? exit (__errstatus) : (void) 0; \
+    })
+#else
+# define __gl_error_call(function, status, ...) \
+    (function) (status, __VA_ARGS__)
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -69,7 +81,7 @@ _GL_CXXALIAS_RPL (error, void,
 # ifndef _GL_NO_INLINE_ERROR
 #  undef error
 #  define error(status, ...) \
-     ((rpl_error)(0, __VA_ARGS__), (status) != 0 ? exit (status) : (void)0)
+     __gl_error_call (rpl_error, status, __VA_ARGS)
 # endif
 #else
 # if ! @HAVE_ERROR@
@@ -81,7 +93,7 @@ _GL_CXXALIAS_SYS (error, void,
                   (int __status, int __errnum, const char *__format, ...));
 # ifndef _GL_NO_INLINE_ERROR
 #  define error(status, ...) \
-     ((error)(0, __VA_ARGS__), (status) != 0 ? exit (status) : (void)0)
+     __gl_error_call (error, status, __VA_ARGS__)
 # endif
 #endif
 #if __GLIBC__ >= 2
@@ -105,7 +117,7 @@ _GL_CXXALIAS_RPL (error_at_line, void,
 # ifndef _GL_NO_INLINE_ERROR
 #  undef error_at_line
 #  define error_at_line(status, ...) \
-     ((rpl_error_at_line)(0, __VA_ARGS__), (status) != 0 ? exit (status) : (void)0)
+     __gl_error_call (rpl_error_at_line, status, __VA_ARGS__)
 # endif
 #else
 # if ! @HAVE_ERROR_AT_LINE@
@@ -119,7 +131,7 @@ _GL_CXXALIAS_SYS (error_at_line, void,
                    unsigned int __lineno, const char *__format, ...));
 # ifndef _GL_NO_INLINE_ERROR
 #  define error_at_line(status, ...) \
-     ((error_at_line)(0, __VA_ARGS__), (status) != 0 ? exit (status) : (void)0)
+     __gl_error_call (error_at_line, status, __VA_ARGS__)
 # endif
 #endif
 _GL_CXXALIASWARN (error_at_line);