public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dak at gnu dot org" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug libc/18604] New: assert macro-expands its argument
Date: Fri, 26 Jun 2015 07:35:00 -0000	[thread overview]
Message-ID: <bug-18604-131@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=18604

            Bug ID: 18604
           Summary: assert macro-expands its argument
           Product: glibc
           Version: 2.21
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: dak at gnu dot org
                CC: drepper.fsp at gmail dot com
  Target Milestone: ---

Created attachment 8394
  --> https://sourceware.org/bugzilla/attachment.cgi?id=8394&action=edit
Sample program showing assert expanding its argument unnecessarily

The attached program produces the output
STRINGIFY(NULL): NULL
STRINGIFY2(NULL): ((void *)0)
a.out: test.c:11: main: Assertion `((void *)0)' failed.

It is a bad idea for assert to be expanding its argument since depending on the
condition it may lead to a completely unreadable mess: in my concrete case of a
failed assertion, I got some indecipherable 8-line output for a failed
assertion assert (SCM_MODULEP (module_));

The problem is that assert.h contains

# define assert(expr)                                                   \
  ((expr)                                                               \
   ? __ASSERT_VOID_CAST (0)                                             \
   : __assert_fail (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))

Here __STRING(expr) needs to be replaced by #expr instead.  If the definition
of __STRING depends on available capabilities, then the check for the
capabilities has to be moved here, like

#if (HAVE_NORMAL_STRINGIFICATION)

# define assert(expr)                                                   \
  ((expr)                                                               \
   ? __ASSERT_VOID_CAST (0)                                             \
   : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))

#else

# define assert(expr)                                                   \
  ((expr)                                                               \
   ? __ASSERT_VOID_CAST (0)                                             \
   : __assert_fail (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))

#endif

An argument intended for stringification must not be passed to another macro
before being stringified or it will get prematurely expanded.  So the general
technique of __STRING is unfeasible when expansion is undesired, which it
clearly is in the case of assert.

Thank you.

-- 
You are receiving this mail because:
You are on the CC list for the bug.


             reply	other threads:[~2015-06-26  7:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-26  7:35 dak at gnu dot org [this message]
2015-06-28 10:09 ` [Bug libc/18604] " dak at gnu dot org
2015-06-28 11:56 ` schwab@linux-m68k.org
2015-06-29 14:49 ` joseph at codesourcery dot com
2015-07-03 12:44 ` dak at gnu dot org
2015-07-09  9:25 ` dak at gnu dot org
2015-07-09 21:31 ` msebor at redhat dot com
2015-07-22 20:24 ` joseph at codesourcery dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-18604-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).