From 9e0dea0293ecb018a877f11f484d551aa9ad1738 Mon Sep 17 00:00:00 2001 From: avolkov Date: Fri, 24 Nov 2006 14:50:52 +0000 Subject: [PATCH] Fixed race condition and potential b/o in char input git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2528 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/src/sc2code/libs/input/sdl/input.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/sc2/src/sc2code/libs/input/sdl/input.c b/sc2/src/sc2code/libs/input/sdl/input.c index af066ed43..b8436e648 100644 --- a/sc2/src/sc2code/libs/input/sdl/input.c +++ b/sc2/src/sc2code/libs/input/sdl/input.c @@ -35,7 +35,8 @@ static int kbdhead=0, kbdtail=0; static wchar_t kbdbuf[KBDBUFSIZE]; static wchar_t lastchar; -static int kbdstate[SDLK_LAST]; +static int kbdstate[SDLK_LAST + 1]; + // +1 for tracking all yet unknown keys static BOOLEAN InputInitialized = FALSE; @@ -315,24 +316,33 @@ ProcessInputEvent (const SDL_Event *Event) ProcessMouseEvent (Event); VControl_HandleEvent (Event); + if (Event->type == SDL_KEYDOWN || Event->type == SDL_KEYUP) { // process character input event, if any SDLKey k = Event->key.keysym.sym; wchar_t map_key = Event->key.keysym.unicode; + if (k < 0 || k > SDLK_LAST) + k = SDLK_LAST; // for unknown keys + if (Event->type == SDL_KEYDOWN) { + int newtail; + // dont care about the non-printable, non-char if (!map_key) return; kbdstate[k]++; - ImmediateInputState.menu[KEY_MENU_ANY]++; - lastchar = map_key; - kbdbuf[kbdtail] = map_key; - kbdtail = (kbdtail + 1) & (KBDBUFSIZE - 1); - if (kbdtail == kbdhead) - kbdhead = (kbdhead + 1) & (KBDBUFSIZE - 1); + newtail = (kbdtail + 1) & (KBDBUFSIZE - 1); + // ignore the char if the buffer is full + if (newtail != kbdhead) + { + kbdbuf[kbdtail] = map_key; + kbdtail = newtail; + lastchar = map_key; + ImmediateInputState.menu[KEY_MENU_ANY]++; + } } else if (Event->type == SDL_KEYUP) {