]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
assert.h: allow gcc to detect assert(a = 1) errors
authorJim Meyering <meyering@fb.com>
Thu, 5 Jun 2014 17:42:05 +0000 (10:42 -0700)
committerJim Meyering <meyering@fb.com>
Sun, 18 Dec 2016 09:30:51 +0000 (01:30 -0800)
* assert/assert.h (assert): Rewrite assert's definition so that
a s/==/=/ typo, e.g., assert(errno = ENOENT) is not hidden from
gcc's -Wparentheses by assert-added parentheses.  The new definition
uses "if (expr) /* empty */; else __assert_fail...", so
gcc -Wall will now detect that type of error in an assert, too.
The __STRICT_ANSI__ disjunct is to make this work also with both
-ansi and  -pedantic, which would reject the use of ({...}).
I would have preferred to use __extension__ to mark that, but
doing so would mistakenly suppress warnings about any extension
in the user-supplied "expr".
E.g., "assert ( ({1;}) )" must continue to evoke a warning.

ChangeLog
assert/assert.h

index 452210e9cabd9932f04209ba401e9f1ed7ca387e..5e49f6ba29f892a39949410430c6e327f5529754 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2016-11-25  Jim Meyering  <meyering@fb.com>
+
+       Let gcc detect assert(a = 1) errors.
+       * assert/assert.h (assert) Rewrite, retaining the old definintion
+       when required, but otherwise putting the expression as-is in an "if"
+       expression (hence, with no added parentheses) within a statement
+       expression.
+
 2016-12-17  Siddhesh Poyarekar  <siddhesh@sourceware.org>
 
        * benchtests/Makefile (binaries-benchset): Depend on libsupport
index 729edeb949ebc5b4962c0ebd1ca8dafaab004e59..0f25131ae444f0e856fa073d86b1ebc397cc97e3 100644 (file)
@@ -82,10 +82,23 @@ extern void __assert (const char *__assertion, const char *__file, int __line)
 
 __END_DECLS
 
-# define assert(expr)                                                  \
-  ((expr)                                                              \
-   ? __ASSERT_VOID_CAST (0)                                            \
-   : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
+/* When possible, define assert so that it does not add extra
+   parentheses around EXPR.  Otherwise, those added parentheses would
+   suppress warnings we'd expect to be detected by gcc's -Wparentheses.  */
+# if !defined __GNUC__ || defined __STRICT_ANSI__
+#  define assert(expr)                                                 \
+    ((expr)                                                            \
+     ? __ASSERT_VOID_CAST (0)                                          \
+     : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
+# else
+#  define assert(expr)                                                 \
+    ({                                                                 \
+      if (expr)                                                                \
+        ; /* empty */                                                  \
+      else                                                             \
+        __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION);  \
+    })
+# endif
 
 # ifdef        __USE_GNU
 #  define assert_perror(errnum)                                                \