libsdl-org / sdl12-compat

An SDL-1.2 compatibility layer that uses SDL 2.0 behind the scenes.
Other
196 stars 40 forks source link

SDL 1.2 headers included <stddef.h> but sdl12-compat no longer does #297

Closed smcv closed 1 year ago

smcv commented 1 year ago

I've been trying to rebuild various Debian packages against sdl12-compat headers, which is likely to be a prerequisite for Debian switching to sdl12-compat (not because we'll actually rebuild them - I know sdl12-compat is binary-compatible with classic SDL 1.2 - but because the release managers will want to know that we can rebuild them if/when they need unrelated bug fixes).

One game, berusky, compiles OK with classic SDL 1.2, but failed to compile with sdl12-compat headers:

In file included from stack.h:31,
                 from level_game.h:32,
                 from berusky.h:131:
level_game.cpp: In constructor 'level_generic::level_generic(ITEM_REPOSITORY*)':
level_game.cpp:107:29: error: expected primary-expression before ',' token
  107 |   assert(offsetof(LEVEL_DISK, signum) == 0);
      |                             ^
level_game.cpp:107:31: error: 'signum' was not declared in this scope; did you mean 'signed'?
  107 |   assert(offsetof(LEVEL_DISK, signum) == 0);
      |                               ^~~~~~
level_game.cpp:107:10: error: 'offsetof' was not declared in this scope
  107 |   assert(offsetof(LEVEL_DISK, signum) == 0);
      |          ^~~~~~~~
level_game.cpp:35:1: note: 'offsetof' is defined in header '<cstddef>'; did you forget to '#include <cstddef>'?
   34 | #include "berusky.h"
  +++ |+#include <cstddef>
   35 |

This appears to be because classic SDL 1.2 has an AC_CHECK_HEADERS for stddef.h (the C equivalent of <cstddef>, with a definition of offsetof), but the substitute SDL_config.h in sdl12-compat doesn't define HAVE_STDDEF_H, resulting in SDL_stdinc.h not including stddef.h.

stddef.h is required by C89, and is unconditionally included by even very portable libraries like libdbus, so it should be safe to define HAVE_STDDEF_H and include <stddef.h> unconditionally on all platforms.

smcv commented 1 year ago

Reproducer:

#include "SDL.h"

struct a {
    int b;
};

int main(void)
{
  return offsetof(struct a, b);
}

gcc -ot t.c $(pkg-config --cflags --libs sdl) with sdl12-compat 1.2.64 ⇒ fails

gcc -ot t.c -Iinclude/SDL without #298 ⇒ fails

gcc -ot t.c -Iinclude/SDL with #298 ⇒ succeeds