home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / std / c / 3282 < prev    next >
Encoding:
Text File  |  1992-12-30  |  2.1 KB  |  53 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!uunet.ca!wildcan!sq!msb
  3. From: msb@sq.sq.com (Mark Brader)
  4. Subject: Overflow in constant expression (was: Standard conformance and GCC 2.3.3)
  5. Message-ID: <1992Dec30.075041.10907@sq.sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <1992Dec30.011211.11409@netcom.com>
  8. Date: Wed, 30 Dec 92 07:50:41 GMT
  9. Lines: 42
  10.  
  11.  
  12. > #2)  GCC fails to detect the error in the following code, even when the
  13. >      -pedantic-errors option is used.
  14. > unsigned long overflow = LONG_MAX + 3; /* ERROR */
  15.  
  16. I presume this is being called erroneous because of the constraint:
  17.  
  18. #  Each constant expression shall evaluate to a constant that
  19. #  is in the range of representable values for its type.
  20.  
  21. in section 3.4 (ANSI) / 6.4 (ISO).  However, 3.4/6.4 also says:
  22.  
  23. #  The semantic rules for evaluation of a constant expression
  24. #  are the same as for non-constant expressions.
  25.  
  26. This presumably includes the assertion in 3.3/6.3:
  27.  
  28. #  If an "exception" occurs during the evaluation of an expression
  29. #  (that is, if the result is not mathematically defined or not in
  30. #  the range of representable values for its type), the behavior
  31. #  is undefined.
  32.  
  33. Since the constraint on the constant-expression applies to the *result*
  34. of evaluating it, but the undefined behavior due to overflow applies
  35. *during* the evaluation, I think "undefined behavior" wins.  That is,
  36. the compiler is allowed to ignore the overflow and compute some value
  37. for the constant-expression that is a valid long value.  Having done
  38. so, it has satisfied the constraint and need not report an error.
  39.  
  40. It may be asked what, in that case, the words "in the range of
  41. representable values for its type" are doing in 3.4/6.4.  And my
  42. answer is that I don't know; the Rationale is silent on the point.
  43. It seems to me that the words are entirely redundant in this context.
  44. Maybe they were an attempt to specify that the expression had to
  45. evaluate without any exceptions occurring, but if so, I think the
  46. attempt was unsuccessful.
  47. -- 
  48. Mark Brader, SoftQuad Inc., Toronto, utzoo!sq!msb, msb@sq.com
  49. #define    MSB(type)    (~(((unsigned type)-1)>>1))
  50.  
  51. This article is in the public domain.
  52.