This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH B1/2] Redefine assert to use its expression
- From: Rich Felker <dalias at libc dot org>
- To: Richard Henderson <rth at twiddle dot net>
- Cc: libc-alpha at sourceware dot org, roland at hack dot frob dot com
- Date: Mon, 2 Mar 2015 18:18:39 -0500
- Subject: Re: [PATCH B1/2] Redefine assert to use its expression
- Authentication-results: sourceware.org; auth=none
- References: <1420827419-18655-1-git-send-email-rth at twiddle dot net> <1420827419-18655-2-git-send-email-rth at twiddle dot net>
On Fri, Jan 09, 2015 at 10:16:58AM -0800, Richard Henderson wrote:
> This also fixes the -Werrors from -DNDEBUG on x86_64, but in
> the opposite direction. We make assert always use its expression.
> This requires removing some existing ifndefs that were working
> around this same problem of unused variables.
>
> There is exactly one compilation difference from patch A1, in
> fork.os, where the THREAD_GETMEM read cannot be optimized away,
> because the asm is marked volatile. (Yet another reason to finish
> code in gcc to expose segments as address spaces, so that this can
> be written in C.)
> [...]
> diff --git a/include/assert.h b/include/assert.h
> index c452667..d7e2759 100644
> --- a/include/assert.h
> +++ b/include/assert.h
> @@ -25,3 +25,10 @@ hidden_proto (__assert_fail)
> hidden_proto (__assert_perror_fail)
> # endif
> #endif
> +
> +#ifdef NDEBUG
> +# undef assert
> +# undef assert_perror
> +# define assert(expr) ((void)(0 && (expr)))
> +# define assert_perror(errnum) ((void)(0 && (errnum)))
> +#endif
This change is non-conforming to the requirements of ISO C, which
state explicitly (C11 7.2p1):
If NDEBUG is defined as a macro name at the point in the source
file where <assert.h> is included, the assert macro is defined
simply as
#define assert(ignore) ((void)0)
There is no allowance for alternate definitions. In particular, the
following program is valid C:
#define NDEBUG
int main() { assert(+++++++++++++++); }
There are presumably also ways you can observe a wrong definition with
stringifying operations at the preprocessor level.
Rich