[gnu.g++.lib.bug] Possible bug in <assert.h>

jj@idris.id.dk (Jesper Joergensen [ris]) (12/21/89)

 Hello Denmark calling,

  I've just considered using the assertion macros in <assert.h> and was
  inspecting the definitions (version 1.36.1) out of pure curiosity. I
  found something which seemed strange to me and decided to report it,
  since it might be a bug.

  The definitions effectuated when NDEBUG is defined are:

	#define assert(ignore)
	#define assertval(ex)  (ex)

  which tells me that  assertval  is intended for use in an expression,
  since this dummy definition returns the value of the test expression
  itself, while  assert  is meant for use as a statement.

  The definition of  assertval  in effect when NDEBUG is undefined is:

    #define assertval(ex) \
            ((ex) ? 1 : \
                  (__eprintf("Failed assertion " #ex " at line %d of `%s'.\n",\
                   __LINE__, __FILE__), abort (), 0))

  but this macro doesn't return the exact value of the given expression
  itself, only an indication that the expression is true. Besides that it is
  completely identical to  assert  in this case. If  assert  is meant for
  use as a statement, then why isn't it defined as a regular  if  statement.

  Is this an error or is it intentional and if so why ?? (side effects ??)

  By the way: I can see what NDEBUG is used for, but is it for user control
  or controlled by the system/compiler ?? Is it used for anything else ??


  That's all for now, I'll return some other time

	Marry Christmas and a Happy new year


	Jesper Jorgensen (known as 'JJ the famous')

	Research associate
	Department of Computer Science
	Technical University of Denmark
	DK-2800 Lyngby
	DENMARK

dl@G.OSWEGO.EDU (Doug Lea) (12/29/89)

Sorry for the delayed reply -- I've been away.

Well, it's less of a bug than a limitation: if you were to change assertval
from

    #define assertval(ex) \
            ((ex) ? 1 : \
                  (__eprintf("Failed assertion " #ex " at line %d of `%s'.\n",\
                   __LINE__, __FILE__), abort (), 0))

to

    #define assertval(ex) \
            ((ex) ? (ex) : \
                  (__eprintf("Failed assertion " #ex " at line %d of `%s'.\n",\
                   __LINE__, __FILE__), abort (), 0))


then it would evaluate ex twice. While it IS possible to overcome
this with gcc/g++ expression-returning compound statements, I didn't
do this, in order to be conservative. Do thing it is worth doing?

-Doug