michaelrsweet / mxml

Tiny XML library.
https://www.msweet.org/mxml
Apache License 2.0
426 stars 157 forks source link

pthread_key_t is assumed to be an integer, which is against POSIX #287

Closed carlo-bramini closed 2 years ago

carlo-bramini commented 2 years ago

I compiled mxml on CYGWIN and I got these messages on screen:

mxml-private.c:143:37: warning: initialization of ‘pthread_key_t’ {aka ‘struct __pthread_key_t *’} from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
  143 | static pthread_key_t    _mxml_key = -1; /* Thread local storage key */
      |                                     ^
mxml-private.c: In function ‘_mxml_fini’:
mxml-private.c:171:17: warning: comparison between pointer and integer
  171 |   if (_mxml_key != -1)
      |                 ^~
mxml-private.c:177:15: warning: assignment to ‘pthread_key_t’ {aka ‘struct __pthread_key_t *’} from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
  177 |     _mxml_key = -1;
      |               ^

This happens because pthread_key_t is an opaque object and it should not be assumed to be an integer type. In my opinion, this is not a good idea because, according to POSIX:

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html

All of the types shall be defined as arithmetic types of an appropriate length, with the following exceptions:

....
pthread_key_t
....

It is true that, at least on CYGWIN, I can bypass the trouble by simply using the TLS APIs for Windows (just need to test __CYGWIN__ in addition to _WIN32 into mxml-private.c) but it would be worth to fix it since other platforms could have this trouble.

michaelrsweet commented 2 years ago

[master 02f3310] Fix POSIX threading support on MingW (Issue #287)