public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] assert.h: allow gcc to detect assert(a = 1) errors
@ 2014-07-16 19:12 Jim Meyering
  2014-07-16 20:15 ` Roland McGrath
  0 siblings, 1 reply; 17+ messages in thread
From: Jim Meyering @ 2014-07-16 19:12 UTC (permalink / raw)
  To: libc-alpha; +Cc: Roland McGrath

From: Jim Meyering <meyering@fb.com>

* 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 the parentheses added by assert.
The new definition uses "if (expr) ; else __assert_fail...", so
gcc -Wall will now detect that type of error in an assert, too.
---
 assert/assert.h | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/assert/assert.h b/assert/assert.h
index 2661391..ffcf60c 100644
--- a/assert/assert.h
+++ b/assert/assert.h
@@ -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 (__STRING(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 (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))
+# else
+#  define assert(expr)							\
+    ({									\
+      if (expr)								\
+        ; /* empty */							\
+      else								\
+        __assert_fail (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION);\
+    })
+# endif

 # ifdef	__USE_GNU
 #  define assert_perror(errnum)						\
-- 
2.0.1

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2016-12-28 12:50 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-16 19:12 [PATCH] assert.h: allow gcc to detect assert(a = 1) errors Jim Meyering
2014-07-16 20:15 ` Roland McGrath
2014-07-16 20:44   ` Jim Meyering
2016-11-24  2:22     ` Jim Meyering
2016-11-24  2:59       ` Zack Weinberg
2016-11-24 13:13         ` Joseph Myers
2016-11-26  6:36           ` Jim Meyering
2016-11-24  7:36       ` Florian Weimer
2016-11-26  6:15         ` Jim Meyering
2016-12-09  3:21           ` Jim Meyering
2016-12-14  5:28             ` Jim Meyering
2016-12-14  9:13           ` Florian Weimer
2016-12-15  0:25             ` Jim Meyering
2016-12-16 12:15               ` Florian Weimer
2016-12-18  9:53                 ` Jim Meyering
2016-12-27 17:47               ` Andreas Schwab
2016-12-28 12:50                 ` Florian Weimer

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).