# Undefined behavior in test/testlock.c

**URL:** <https://discourse.libsdl.org/t/undefined-behavior-in-test-testlock-c/13330>\
**Category:** SDL Development\
**Created:** [May 6, 2006, 3:09pm UTC](https://discourse.libsdl.org/t/undefined-behavior-in-test-testlock-c/13330 "2006-05-06T15:09:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Peter](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@Marc\_Peter](https://discourse.libsdl.org/u/Marc_Peter)\
**Post date:** [May 6, 2006, 3:09pm UTC](https://discourse.libsdl.org/t/undefined-behavior-in-test-testlock-c/13330/1 "2006-05-06T15:09:01Z")

</div>

Hello,

reading the C99 standard, section 7.14.1.1 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 @@  
SDL\_DestroyMutex(mutex);  
exit(sig);  
}  
-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 \*/  
SDL\_Delay(10);
- 

```
  if (SDL_ThreadID() == mainthread && doterminate) {

```

- 

```
  	printf("Process %u: raising SIGTERM\n", SDL_ThreadID());

```

- 

```
  	raise(SIGTERM);

```

- 

```
  }

```

}  
return(0);  
}  
--------------- 8\< ---------------

Cheers  
Marc

–  
Marc Peter

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

---

<div class="post-metadata">

**Author:** ![Marc\_Peter](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@Marc\_Peter](https://discourse.libsdl.org/u/Marc_Peter)\
**Post date:** [May 8, 2006, 9:57pm UTC](https://discourse.libsdl.org/t/undefined-behavior-in-test-testlock-c/13330/2 "2006-05-08T21:57:25Z")

</div>

Hi,

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  
list.

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
