Type problem in SDL_systhread.c

I was trying to update the SDL.dll project file for Borland C++ Builder, which hasn’t been updated since 2003, for a modern version. After fixing up the file list, I tried to compile, and got an error on \src\thread\win32\SDL_systhread.c:

[BCC32 Error] SDL_systhread.c(139): E2342 Type mismatch in parameter ‘lpStartAddress’ (wanted ‘unsigned long (__stdcall *)(void *)’, got ‘unsigned int (__stdcall *)(void *)’)

if (pfnBeginThread) {
    thread->handle =
        (SYS_ThreadHandle) pfnBeginThread(NULL, 0, RunThread,
                                          pThreadParms, 0, &threadid);
} else {
    thread->handle =
        CreateThread(NULL, 0, RunThread, pThreadParms, 0, &threadid); //error occurs on 3rd parameter of this call
}

RunThread is defined as static unsigned __stdcall RunThread(void *data). Apparently “unsigned” means “unsigned int”, and it’s trying to send it to a WinAPI function that expects an unsigned long return type. For whatever reason, this compiles under Visual Studio, even though that probably has the potential for data loss or data corruption. It should probably be fixed, though…

http://msdn.microsoft.com/en-us/library/ms686736(VS.85).aspx

says it should be “DWORD”. On all x86 and x86_64 architectures, a DWORD is an unsigned int. On IA64, it may be different.

It is my understanding that all the Microsoft file format headers (for BITMAP and many others) would break if DWORD changed size, so I’m quite sure it is always exactly 32 bits, even on IA64.

P.S. extremely few people in game development have access to an IA64 computer (unlike the nearly ubiquitous x86_64 processors), desktop computers do not use them.On 02/09/2010 07:39 PM, nfries88 wrote:

http://msdn.microsoft.com/en-us/library/ms686736(VS.85).aspx

says it should be “DWORD”. On all x86 and x86_64 architectures, a DWORD
is an unsigned int. On IA64, it may be different.


LordHavoc
Author of DarkPlaces Quake1 engine - http://icculus.org/twilight/darkplaces
Co-designer of Nexuiz - http://alientrap.org/nexuiz
"War does not prove who is right, it proves who is left." - Unknown
"Any sufficiently advanced technology is indistinguishable from a rigged demo." - James Klass
"A game is a series of interesting choices." - Sid Meier

Hi,

something bothers me from this code snippet.

Why is CreateThread being used instead of _beginthread(ex)?

Usually one should not call CreateThread() directly, because it does not
take into
account the initialization of the C run time library for the new thread.–
Paulo

On Wed, Feb 10, 2010 at 12:29 AM, Mason Wheeler wrote:

I was trying to update the SDL.dll project file for Borland C++ Builder,
which hasn’t been updated since 2003, for a modern version. After fixing up
the file list, I tried to compile, and got an error on
\src\thread\win32\SDL_systhread.c:

[BCC32 Error] SDL_systhread.c(139): E2342 Type mismatch in parameter
’lpStartAddress’ (wanted ‘unsigned long (__stdcall *)(void *)’, got
’unsigned int (__stdcall *)(void *)’)

if (pfnBeginThread) {
thread->handle =
(SYS_ThreadHandle) pfnBeginThread(NULL, 0, RunThread,
pThreadParms, 0, &threadid);
} else {
thread->handle =
CreateThread(NULL, 0, RunThread, pThreadParms, 0, &threadid);
//error occurs on 3rd parameter of this call
}

RunThread is defined as static unsigned __stdcall RunThread(void *data).
Apparently “unsigned” means “unsigned int”, and it’s trying to send it to a
WinAPI function that expects an unsigned long return type. For whatever
reason, this compiles under Visual Studio, even though that probably has the
potential for data loss or data corruption. It should probably be fixed,
though…


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Forest Hale wrote:> On 02/09/2010 07:39 PM, nfries88 wrote:

http://msdn.microsoft.com/en-us/library/ms686736(VS.85).aspx

says it should be “DWORD”. On all x86 and x86_64 architectures, a DWORD
is an unsigned int. On IA64, it may be different.

It is my understanding that all the Microsoft file format headers (for BITMAP and many others) would break if DWORD changed size, so I’m quite sure it is always exactly 32 bits, even on IA64.

P.S. extremely few people in game development have access to an IA64 computer (unlike the nearly ubiquitous x86_64 processors), desktop computers do not use them.

I wrote that a little backwards, I meant that an unsigned int might not be of the same size as a DWORD (32-bits) on IA64. DWORD is meant to always be 32-bits.

@Paulo Pinto: You raise an excellent point.

Because that code path is used when SDL is built without C runtime support. :)On Tue, Feb 9, 2010 at 11:35 PM, Paulo Pinto wrote:

Hi,

something bothers me from this code snippet.

Why is CreateThread being used instead of _beginthread(ex)?

Usually one should not call CreateThread() directly, because it does not
take into
account the initialization of the C run time library for the new thread.


Paulo

On Wed, Feb 10, 2010 at 12:29 AM, Mason Wheeler wrote:

I was trying to update the SDL.dll project file for Borland C++ Builder,
which hasn’t been updated since 2003, for a modern version. ?After fixing up
the file list, I tried to compile, and got an error on
\src\thread\win32\SDL_systhread.c:

[BCC32 Error] SDL_systhread.c(139): E2342 Type mismatch in parameter
’lpStartAddress’ (wanted ‘unsigned long (__stdcall *)(void *)’, got
’unsigned int (__stdcall *)(void *)’)

? ?if (pfnBeginThread) {
? ? ? ?thread->handle =
? ? ? ? ? ?(SYS_ThreadHandle) pfnBeginThread(NULL, 0, RunThread,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?pThreadParms, 0, &threadid);
? ?} else {
? ? ? ?thread->handle =
? ? ? ? ? ?CreateThread(NULL, 0, RunThread, pThreadParms, 0, &threadid);
//error occurs on 3rd parameter of this call
? ?}

RunThread is defined as static unsigned __stdcall RunThread(void *data).
?Apparently “unsigned” means “unsigned int”, and it’s trying to send it to a
WinAPI function that expects an unsigned long return type. ?For whatever
reason, this compiles under Visual Studio, even though that probably has the
potential for data loss or data corruption. ?It should probably be fixed,
though…


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


-Sam Lantinga, Founder and President, Galaxy Gameworks LLC

This is fixed for SDL 1.3 in subversion, thanks!On Tue, Feb 9, 2010 at 3:29 PM, Mason Wheeler wrote:

I was trying to update the SDL.dll project file for Borland C++ Builder, which hasn’t been updated since 2003, for a modern version. ?After fixing up the file list, I tried to compile, and got an error on \src\thread\win32\SDL_systhread.c:

[BCC32 Error] SDL_systhread.c(139): E2342 Type mismatch in parameter ‘lpStartAddress’ (wanted ‘unsigned long (__stdcall *)(void *)’, got ‘unsigned int (__stdcall *)(void *)’)

? ?if (pfnBeginThread) {
? ? ? ?thread->handle =
? ? ? ? ? ?(SYS_ThreadHandle) pfnBeginThread(NULL, 0, RunThread,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?pThreadParms, 0, &threadid);
? ?} else {
? ? ? ?thread->handle =
? ? ? ? ? ?CreateThread(NULL, 0, RunThread, pThreadParms, 0, &threadid); //error occurs on 3rd parameter of this call
? ?}

RunThread is defined as static unsigned __stdcall RunThread(void *data). ?Apparently “unsigned” means “unsigned int”, and it’s trying to send it to a WinAPI function that expects an unsigned long return type. ?For whatever reason, this compiles under Visual Studio, even though that probably has the potential for data loss or data corruption. ?It should probably be fixed, though…


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


-Sam Lantinga, Founder and President, Galaxy Gameworks LLC

Another similar issue exists in SDL_win32events.c.

[BCC32 Error] SDL_win32events.c(126): E2342 Type mismatch in parameter ‘lpPrevWndFunc’ (wanted ‘int (__stdcall *)()’, got ‘long (__stdcall *)(void *,unsigned int,unsigned int,long)’)

Not quite sure what’s going on here, but it’s looking for a function pointer that takes no arguments, and the code’s passing one that takes four arguments. That looks like a sure way to corrupt data and break the stack. How does this even work when you compile it under Visual C++? Is there some weird configuration thing going on that I’m not aware of?>----- Original Message ----

From: Sam Lantinga
Subject: Re: [SDL] Type problem in SDL_systhread.c

This is fixed for SDL 1.3 in subversion, thanks!

On Tue, Feb 9, 2010 at 3:29 PM, Mason Wheeler <@Mason_Wheeler> wrote:

I was trying to update the SDL.dll project file for Borland C++ Builder, which hasn’t been updated since 2003, for a modern version. After fixing up the file list, I tried to compile, and got an error on \src\thread\win32\SDL_systhread.c:

[BCC32 Error] SDL_systhread.c(139): E2342 Type mismatch in parameter ‘lpStartAddress’ (wanted ‘unsigned long (__stdcall *)(void *)’, got ‘unsigned int (__stdcall *)(void *)’)

if (pfnBeginThread) {
thread->handle =
(SYS_ThreadHandle) pfnBeginThread(NULL, 0, RunThread,
pThreadParms, 0, &threadid);
} else {
thread->handle =
CreateThread(NULL, 0, RunThread, pThreadParms, 0, &threadid); //error occurs on 3rd parameter of this call
}

RunThread is defined as static unsigned __stdcall RunThread(void *data). Apparently “unsigned” means “unsigned int”, and it’s trying to send it to a WinAPI function that expects an unsigned long return type. For whatever reason, this compiles under Visual Studio, even though that probably has the potential for data loss or data corruption. It should probably be fixed, though…

From MSDN:
If STRICT is defined, the lpPrevWndFunc parameter has the data type
WNDPROC. The WNDPROC type is declared as follows:

LRESULT (CALLBACK* WNDPROC) (HWND, UINT, WPARAM, LPARAM);

If STRICT is not defined, the lpPrevWndFunc parameter has the data
type FARPROC. The FARPROC type is declared as follows:

int (FAR WINAPI * FARPROC) ()

I just checked in a change that defines STRICT explicitly.On Sun, Feb 14, 2010 at 7:34 AM, Mason Wheeler wrote:

Another similar issue exists in SDL_win32events.c.

[BCC32 Error] SDL_win32events.c(126): E2342 Type mismatch in parameter ‘lpPrevWndFunc’ (wanted ‘int (__stdcall *)()’, got ‘long (__stdcall *)(void *,unsigned int,unsigned int,long)’)

Not quite sure what’s going on here, but it’s looking for a function pointer that takes no arguments, and the code’s passing one that takes four arguments. ?That looks like a sure way to corrupt data and break the stack. ?How does this even work when you compile it under Visual C++? ?Is there some weird configuration thing going on that I’m not aware of?

----- Original Message ----
From: Sam Lantinga <@slouken>
Subject: Re: [SDL] Type problem in SDL_systhread.c

This is fixed for SDL 1.3 in subversion, thanks!

On Tue, Feb 9, 2010 at 3:29 PM, Mason Wheeler wrote:

I was trying to update the SDL.dll project file for Borland C++ Builder, which hasn’t been updated since 2003, for a modern version. ?After fixing up the file list, I tried to compile, and got an error on \src\thread\win32\SDL_systhread.c:

[BCC32 Error] SDL_systhread.c(139): E2342 Type mismatch in parameter ‘lpStartAddress’ (wanted ‘unsigned long (__stdcall *)(void *)’, got ‘unsigned int (__stdcall *)(void *)’)

? ?if (pfnBeginThread) {
? ? ? ?thread->handle =
? ? ? ? ? ?(SYS_ThreadHandle) pfnBeginThread(NULL, 0, RunThread,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?pThreadParms, 0, &threadid);
? ?} else {
? ? ? ?thread->handle =
? ? ? ? ? ?CreateThread(NULL, 0, RunThread, pThreadParms, 0, &threadid); //error occurs on 3rd parameter of this call
? ?}

RunThread is defined as static unsigned __stdcall RunThread(void *data). ?Apparently “unsigned” means “unsigned int”, and it’s trying to send it to a WinAPI function that expects an unsigned long return type. ?For whatever reason, this compiles under Visual Studio, even though that probably has the potential for data loss or data corruption. ?It should probably be fixed, though…


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


-Sam Lantinga, Founder and President, Galaxy Gameworks LLC

This fix breaks the other half of the conditional, because pfnBeginThread is still expecting the old function pointer type. Gimme a few minutes and I’ll come up with a patch that works.>----- Original Message ----

From: Sam Lantinga
Subject: Re: [SDL] Type problem in SDL_systhread.c

This is fixed for SDL 1.3 in subversion, thanks!

On Tue, Feb 9, 2010 at 3:29 PM, Mason Wheeler <@Mason_Wheeler> wrote:

I was trying to update the SDL.dll project file for Borland C++ Builder, which hasn’t been updated since 2003, for a modern version. After fixing up the file list, I tried to compile, and got an error on \src\thread\win32\SDL_systhread.c:

[BCC32 Error] SDL_systhread.c(139): E2342 Type mismatch in parameter ‘lpStartAddress’ (wanted ‘unsigned long (__stdcall *)(void *)’, got ‘unsigned int (__stdcall *)(void *)’)

if (pfnBeginThread) {
thread->handle =
(SYS_ThreadHandle) pfnBeginThread(NULL, 0, RunThread,
pThreadParms, 0, &threadid);
} else {
thread->handle =
CreateThread(NULL, 0, RunThread, pThreadParms, 0, &threadid); //error occurs on 3rd parameter of this call
}

RunThread is defined as static unsigned __stdcall RunThread(void *data). Apparently “unsigned” means “unsigned int”, and it’s trying to send it to a WinAPI function that expects an unsigned long return type. For whatever reason, this compiles under Visual Studio, even though that probably has the potential for data loss or data corruption. It should probably be fixed, though…

This fix breaks the other half of the conditional, because pfnBeginThread is still expecting the old function pointer type. Gimme a few minutes and I’ll come up with a patch that works.

OK, turns out the only change needed to make it compile is to change the definition in SDL_thread.h, line 91 from “unsigned (__stdcall * func)” to “unsigned long (__stdcall * func)”.>----- Original Message ----

From: Mason Wheeler <@Mason_Wheeler>
Subject: Re: [SDL] Type problem in SDL_systhread.c