Eric Blake | 3 Jul 14:10
Gravatar

Re: Fix UINT{8,16}_C


According to Corinna Vinschen on 7/3/2006 3:41 AM:
>   
> 
> I have checked the stdint.h headers on glibc 2.3.4 and 2.4, as well as
> on Solaris 10, NetBSD, FreeBSD and OpenBSD.  Only FreeBSD and OpenBSD
> define them as just x, all others as x##U, one way or the other.

And gnulib rejects Solaris 10 and glibc's versions as buggy as well:

http://lists.gnu.org/archive/html/bug-gnulib/2006-06/msg00118.html

> 
> ISO/IEC 9899:TC2 (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf)
> has the following to say:
> 
>   7.18.4.1 Macros for minimum-width integer constants
> 
>   The macro INTN_C(value) shall expand to an integer constant expression
>   corresponding to the type int_leastN_t.

The problem is that there is no integer constant expression for unsigned
char; instead, you get an integer constant expression for the type that
unsigned char promotes to.  Therefore, UINT8_C should give an int, not
unsigned int.

This snippet from gnulib is valid C code, but fails if you use the wrong
type specifier:

  /* Detect bugs in glibc 2.4 and Solaris 10 stdint.h, among others.  */
  int check_UINT8_C:
	(-1 < UINT8_C (0)) == (-1 < (uint_least8_t) 0) ? 1 : -1;
  int check_UINT16_C:
	(-1 < UINT16_C (0)) == (-1 < (uint_least16_t) 0) ? 1 : -1;

--
Life is short - so eat dessert first!

Eric Blake             ebb9 <at> byu.net

Gmane