home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.std.c
- Path: sparky!uunet!uunet.ca!wildcan!sq!msb
- From: msb@sq.sq.com (Mark Brader)
- Subject: Overflow in constant expression (was: Standard conformance and GCC 2.3.3)
- Message-ID: <1992Dec30.075041.10907@sq.sq.com>
- Organization: SoftQuad Inc., Toronto, Canada
- References: <1992Dec30.011211.11409@netcom.com>
- Date: Wed, 30 Dec 92 07:50:41 GMT
- Lines: 42
-
-
- > #2) GCC fails to detect the error in the following code, even when the
- > -pedantic-errors option is used.
- >
- > unsigned long overflow = LONG_MAX + 3; /* ERROR */
-
- I presume this is being called erroneous because of the constraint:
-
- # Each constant expression shall evaluate to a constant that
- # is in the range of representable values for its type.
-
- in section 3.4 (ANSI) / 6.4 (ISO). However, 3.4/6.4 also says:
-
- # The semantic rules for evaluation of a constant expression
- # are the same as for non-constant expressions.
-
- This presumably includes the assertion in 3.3/6.3:
-
- # If an "exception" occurs during the evaluation of an expression
- # (that is, if the result is not mathematically defined or not in
- # the range of representable values for its type), the behavior
- # is undefined.
-
- Since the constraint on the constant-expression applies to the *result*
- of evaluating it, but the undefined behavior due to overflow applies
- *during* the evaluation, I think "undefined behavior" wins. That is,
- the compiler is allowed to ignore the overflow and compute some value
- for the constant-expression that is a valid long value. Having done
- so, it has satisfied the constraint and need not report an error.
-
- It may be asked what, in that case, the words "in the range of
- representable values for its type" are doing in 3.4/6.4. And my
- answer is that I don't know; the Rationale is silent on the point.
- It seems to me that the words are entirely redundant in this context.
- Maybe they were an attempt to specify that the expression had to
- evaluate without any exceptions occurring, but if so, I think the
- attempt was unsuccessful.
- --
- Mark Brader, SoftQuad Inc., Toronto, utzoo!sq!msb, msb@sq.com
- #define MSB(type) (~(((unsigned type)-1)>>1))
-
- This article is in the public domain.
-