Fixed bug #1056 (Frequent crashes in Touch events by simply touching the screen)
authorSam Lantinga <slouken@libsdl.org>
Fri, 28 Jan 2011 10:21:58 -0800
changeset 5124d79ff339d1f2
parent 5123 dc0dfdd58f27
child 5125 b6c7a6b07ebf
Fixed bug #1056 (Frequent crashes in Touch events by simply touching the screen)

Joseba GarcĂ­a Echebarria 2010-12-15 01:55:22 PST

I believe the crash is caused by a check not being performed on wether an
SDL_Touch element is NULL before using it in the SDL_SendTouchMotion function
in src/events/SDL_touch.c around line 400.
Judging from the rest of the code, there's a missing

if (!touch) {
return 0;
}

before using "touch" as SDL_GetFinger(), SDL_GetFingerIndexId() use
touch->num_fingers without checking.

I can attach a patch if you like. It seems pretty straightforward, though.

I have yet to discover why touch is being returned as NULL as this error is
only triggered when an actual gesture has been performed, maybe something
related to SDL_AddTouch()?
src/events/SDL_touch.c
     1.1 --- a/src/events/SDL_touch.c	Thu Jan 27 22:44:08 2011 -0800
     1.2 +++ b/src/events/SDL_touch.c	Fri Jan 28 10:21:58 2011 -0800
     1.3 @@ -397,15 +397,16 @@
     1.4                      float xin, float yin, float pressurein)
     1.5  {
     1.6      int index = SDL_GetTouchIndexId(id);
     1.7 -    SDL_Touch *touch = SDL_GetTouch(id);
     1.8 -    SDL_Finger *finger = SDL_GetFinger(touch,fingerid);
     1.9 +    SDL_Touch *touch;
    1.10 +    SDL_Finger *finger;
    1.11      int posted;
    1.12      Sint16 xrel, yrel;
    1.13      float x_max = 0, y_max = 0;
    1.14 -        Uint16 x;
    1.15 -        Uint16 y;
    1.16 -        Uint16 pressure;
    1.17 +    Uint16 x;
    1.18 +    Uint16 y;
    1.19 +    Uint16 pressure;
    1.20      
    1.21 +    touch = SDL_GetTouch(id);
    1.22      if (!touch) {
    1.23        return SDL_TouchNotFoundError(id);
    1.24      }
    1.25 @@ -418,6 +419,7 @@
    1.26          return 0;
    1.27      }
    1.28      
    1.29 +    finger = SDL_GetFinger(touch,fingerid);
    1.30      if(finger == NULL || !finger->down) {
    1.31          return SDL_SendFingerDown(id,fingerid,SDL_TRUE,xin,yin,pressurein);        
    1.32      } else {
    1.33 @@ -496,14 +498,16 @@
    1.34          return posted;
    1.35      }
    1.36  }
    1.37 +
    1.38  int
    1.39  SDL_SendTouchButton(SDL_TouchID id, Uint8 state, Uint8 button)
    1.40  {
    1.41 -    SDL_Touch *touch = SDL_GetTouch(id);
    1.42 +    SDL_Touch *touch;
    1.43      int posted;
    1.44      Uint32 type;
    1.45  
    1.46      
    1.47 +    touch = SDL_GetTouch(id);
    1.48      if (!touch) {
    1.49        return SDL_TouchNotFoundError(id);
    1.50      }