asctime.c

Paul Eggert eggert at CS.UCLA.EDU
Tue Aug 3 15:01:43 UTC 2004


"Clive D.W. Feather" <clive at demon.net> writes:

> Can you give me an example?

Sure.  Here's an example taken from the code I happened to be looking
at 30 seconds before reading your email.  It's taken from GNU
coreutils "od.c".  I've paraphrased the code slightly to simplify it.

  enum size_spec { NO_SIZE, CHAR, SHORT, INT, LONG } ;
  enum size_spec integral_type_size[sizeof (long) + 1];
  for (i = 0; i <= MAX_INTEGRAL_TYPE_SIZE; i++)
    integral_type_size[i] = NO_SIZE;
  integral_type_size[sizeof (char)] = CHAR;
  integral_type_size[sizeof (short)] = SHORT;
  integral_type_size[sizeof (int)] = INT;
  integral_type_size[sizeof (long)] = LONG;

This code has undefined behavior if, for example, sizeof (long) is 4
and sizeof (int) is 8.

The code in difftime.c is a bit more subtle than this, and now that I
look at it more carefully it can't strictly be justified in terms of
either C89 or C99 (though it is true on all platforms I know about).
However, I'd say that the general principle that sizeof(int) <=
sizeof(long) is hardwired into a lot of real-world code.

If there aren't any real implementations with sizeof(long) <
sizeof(int), then this is only of academic interest.  Still, it's
strange that this longstanding requirement would get removed from the
standard.  After all, it's a natural assumption.



More information about the tz mailing list