Undefined behavior in test/testlock.c


reading the C99 standard, section paragraph 5, I infer that
calling raise in a signal handler exercises undefined behavior.

I came across this when porting SDL (SVN trunk) to OpenWatcom/Win32.
Everything is fine so far, except some missing calling convention
specifiers and testlock crashing on exit. (More on this in another thread.)

Here is a patch that fixes the terminate function for the testlock test.
Note, that I left closemutex alone, although it also boldly exercises
undefined behavior. There might be a line-wrap near the end of the patch
that Thunderbird inserted:

--------------- 8< ---------------
Index: testlock.c===================================================================
— testlock.c (revision 2328)
+++ testlock.c (working copy)
@@ -14,6 +14,7 @@
static SDL_mutex *mutex = NULL;
static Uint32 mainthread;
static SDL_Thread *threads[6];
+static volatile sig_atomic_t doterminate = 0;


  • SDL_Quit() shouldn’t be used with atexit() directly because
    @@ -31,8 +32,7 @@

void terminate(int sig)

  • printf(“Process %u: raising SIGTERM\n”, SDL_ThreadID());
  • raise(SIGTERM);
  • doterminate = 1;
    void closemutex(int sig)
    @@ -44,7 +44,7 @@
    -int Run(void *data)
    +int SDLCALL Run(void data)
    if ( SDL_ThreadID() == mainthread )
    signal(SIGTERM, closemutex);
    @@ -63,6 +63,10 @@
    If this sleep isn’t done, then threads may starve */
  •   if (SDL_ThreadID() == mainthread && doterminate) {
  •   	printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
  •   	raise(SIGTERM);
  •   }
    --------------- 8< ---------------


Marc Peter

PGP: http://pgpkeys.pca.dfn.de/pks/lookup?search=0xD417478C


answering my own post, this is already in Subversion (as part of the
patch delivered in bug #215). The parent message got held back by the
moderators, because it was sent before I had registered with the mailing

Regarding the OpenWatcom port, it’s also available in the Subversion
trunk (see bug #216). I’m not opening a new thread on this issue.
Please let me know if you have questions or comments.

Best regards–
Marc Peter