This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH B1/2] Redefine assert to use its expression

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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]