SDL_Keycode value range

Hi,

does it make sense that the SDL_Keycode value range is signed (Sint32)
in SDL2 (rev. 6303)? I did not check for all implementation details, but
at least SDL_GetKeyName () can cause SIGSEGVs with that type, if certain
negative values are provided:

#include <SDL.h>

int main (int argc, char argv[])
{
SDL_Keycode val = -88; /
perfectly valid (technical) assignment */
printf ("’%d’\n", ((SDL_Scancode)(val & ~SDLK_SCANCODE_MASK)));
printf ("’%s’\n", SDL_GetKeyName (val));
return 0;
}

Since the line if (key & SDLK_SCANCODE_MASK) in SDL_GetKeyName()
evaluates to something !0 with negative ranges, SDL_GetScancodeName()
might try to access the SDL_scancodes_names[] array with a negative
index.

Wouldn’t it make more sense to use a typedef Uint32 SDL_Keycode; instead
or will that release the dragons? If it does not make sense, I’d propose
an additional check in SDL_GetKeyName() to catch the SIGSEGV:

if (key & SDLK_SCANCODE_MASK) {
if (((SDL_Scancode) (key & ~SDLK_SCANCODE_MASK)) < SDL_NUM_SCANCODES)
return SDL_GetScancodeName((SDL_Scancode) (key & ~SDLK_SCANCODE_MASK));
else
return “”;
}

Regards
Marcus
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20120321/e777b17e/attachment.pgp