This is the mail archive of the mailing list for the eCos 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]

[Bug 1001915] New: Invalid assert definition

Please do not reply to this email, use the link below.

            Bug ID: 1001915
           Summary: Invalid assert definition
           Product: eCos
           Version: 3.0
            Target: All
  Architecture/Host All
            Status: UNCONFIRMED
          Severity: major
          Priority: low
         Component: POSIX

According to ISO C, the assert macro must be an expression but it is defined as
an statement (do { } while(0)). This definition causes trouble in several
situation where "assert" is used as an expression.


"The assert() macro shall insert diagnostics into programs; it shall expand to
a void expression."

Right now it is defined as follows:

# define assert( __bool )                                                 \
    do {                                                                  \
        if (0 == (__bool)) {                                              \
            fprintf( stderr, "User assertion failed: \"%s\", at %s:%d\n", \
                         #__bool, __FILE__, __LINE__);                    \
            abort();                                                      \
        }                                                                 \
    } while(0)

I propose changing it to something like this:

# define assert(__bool)                          \
  ((__bool)                                      \
   ? (void)(0)                                   \
   : assert_failed(#__bool, __FILE__, __LINE__)

And then define assert_failed as something like:

void assert_failed(const char* assertion, const char* file, const char* line )
    fprintf( stderr, "User assertion failed: \"%s\", at %s:%d\n",
                  assertion, file, line );

This way, assert always evaluates to a void expression, as demanded by the

(PS: The whole fix would take changes in other macros, I'm just illustrating
the problem and the solution).

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

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