Fix bug 545, by returning early whenever an error occurs when initializing joystick structure SDL-1.2
authorPatrice Mandin <patmandin@gmail.com>
Sat, 19 Jul 2008 11:54:55 +0000
branchSDL-1.2
changeset 41476bdec986f2f6
parent 4146 dc726b233f5f
child 4148 3e7f2edce658
Fix bug 545, by returning early whenever an error occurs when initializing joystick structure
src/joystick/SDL_joystick.c
     1.1 --- a/src/joystick/SDL_joystick.c	Sat Jul 05 18:27:27 2008 +0000
     1.2 +++ b/src/joystick/SDL_joystick.c	Sat Jul 19 11:54:55 2008 +0000
     1.3 @@ -112,64 +112,67 @@
     1.4  
     1.5  	/* Create and initialize the joystick */
     1.6  	joystick = (SDL_Joystick *)SDL_malloc((sizeof *joystick));
     1.7 -	if ( joystick != NULL ) {
     1.8 -		SDL_memset(joystick, 0, (sizeof *joystick));
     1.9 -		joystick->index = device_index;
    1.10 -		if ( SDL_SYS_JoystickOpen(joystick) < 0 ) {
    1.11 -			SDL_free(joystick);
    1.12 -			joystick = NULL;
    1.13 -		} else {
    1.14 -			if ( joystick->naxes > 0 ) {
    1.15 -				joystick->axes = (Sint16 *)SDL_malloc
    1.16 -					(joystick->naxes*sizeof(Sint16));
    1.17 -			}
    1.18 -			if ( joystick->nhats > 0 ) {
    1.19 -				joystick->hats = (Uint8 *)SDL_malloc
    1.20 -					(joystick->nhats*sizeof(Uint8));
    1.21 -			}
    1.22 -			if ( joystick->nballs > 0 ) {
    1.23 -				joystick->balls = (struct balldelta *)SDL_malloc
    1.24 -				  (joystick->nballs*sizeof(*joystick->balls));
    1.25 -			}
    1.26 -			if ( joystick->nbuttons > 0 ) {
    1.27 -				joystick->buttons = (Uint8 *)SDL_malloc
    1.28 -					(joystick->nbuttons*sizeof(Uint8));
    1.29 -			}
    1.30 -			if ( ((joystick->naxes > 0) && !joystick->axes)
    1.31 -			  || ((joystick->nhats > 0) && !joystick->hats)
    1.32 -			  || ((joystick->nballs > 0) && !joystick->balls)
    1.33 -			  || ((joystick->nbuttons > 0) && !joystick->buttons)) {
    1.34 -				SDL_OutOfMemory();
    1.35 -				SDL_JoystickClose(joystick);
    1.36 -				joystick = NULL;
    1.37 -			}
    1.38 -			if ( joystick->axes ) {
    1.39 -				SDL_memset(joystick->axes, 0,
    1.40 -					joystick->naxes*sizeof(Sint16));
    1.41 -			}
    1.42 -			if ( joystick->hats ) {
    1.43 -				SDL_memset(joystick->hats, 0,
    1.44 -					joystick->nhats*sizeof(Uint8));
    1.45 -			}
    1.46 -			if ( joystick->balls ) {
    1.47 -				SDL_memset(joystick->balls, 0,
    1.48 -				  joystick->nballs*sizeof(*joystick->balls));
    1.49 -			}
    1.50 -			if ( joystick->buttons ) {
    1.51 -				SDL_memset(joystick->buttons, 0,
    1.52 -					joystick->nbuttons*sizeof(Uint8));
    1.53 -			}
    1.54 -		}
    1.55 +	if ( !joystick ) {
    1.56 +		return(NULL);
    1.57  	}
    1.58 -	if ( joystick ) {
    1.59 -		/* Add joystick to list */
    1.60 -		++joystick->ref_count;
    1.61 -		SDL_Lock_EventThread();
    1.62 -		for ( i=0; SDL_joysticks[i]; ++i )
    1.63 -			/* Skip to next joystick */;
    1.64 -		SDL_joysticks[i] = joystick;
    1.65 -		SDL_Unlock_EventThread();
    1.66 +
    1.67 +	SDL_memset(joystick, 0, (sizeof *joystick));
    1.68 +	joystick->index = device_index;
    1.69 +	if ( SDL_SYS_JoystickOpen(joystick) < 0 ) {
    1.70 +		SDL_free(joystick);
    1.71 +		return(NULL);
    1.72  	}
    1.73 +
    1.74 +	if ( joystick->naxes > 0 ) {
    1.75 +		joystick->axes = (Sint16 *)SDL_malloc
    1.76 +			(joystick->naxes*sizeof(Sint16));
    1.77 +	}
    1.78 +	if ( joystick->nhats > 0 ) {
    1.79 +		joystick->hats = (Uint8 *)SDL_malloc
    1.80 +			(joystick->nhats*sizeof(Uint8));
    1.81 +	}
    1.82 +	if ( joystick->nballs > 0 ) {
    1.83 +		joystick->balls = (struct balldelta *)SDL_malloc
    1.84 +			(joystick->nballs*sizeof(*joystick->balls));
    1.85 +	}
    1.86 +	if ( joystick->nbuttons > 0 ) {
    1.87 +		joystick->buttons = (Uint8 *)SDL_malloc
    1.88 +			(joystick->nbuttons*sizeof(Uint8));
    1.89 +	}
    1.90 +	if ( ((joystick->naxes > 0) && !joystick->axes)
    1.91 +	  || ((joystick->nhats > 0) && !joystick->hats)
    1.92 +	  || ((joystick->nballs > 0) && !joystick->balls)
    1.93 +	  || ((joystick->nbuttons > 0) && !joystick->buttons)) {
    1.94 +		SDL_OutOfMemory();
    1.95 +		SDL_JoystickClose(joystick);
    1.96 +		return(NULL);
    1.97 +	}
    1.98 +
    1.99 +	if ( joystick->axes ) {
   1.100 +		SDL_memset(joystick->axes, 0,
   1.101 +			joystick->naxes*sizeof(Sint16));
   1.102 +	}
   1.103 +	if ( joystick->hats ) {
   1.104 +		SDL_memset(joystick->hats, 0,
   1.105 +			joystick->nhats*sizeof(Uint8));
   1.106 +	}
   1.107 +	if ( joystick->balls ) {
   1.108 +		SDL_memset(joystick->balls, 0,
   1.109 +			joystick->nballs*sizeof(*joystick->balls));
   1.110 +	}
   1.111 +	if ( joystick->buttons ) {
   1.112 +		SDL_memset(joystick->buttons, 0,
   1.113 +			joystick->nbuttons*sizeof(Uint8));
   1.114 +	}
   1.115 +
   1.116 +	/* Add joystick to list */
   1.117 +	++joystick->ref_count;
   1.118 +	SDL_Lock_EventThread();
   1.119 +	for ( i=0; SDL_joysticks[i]; ++i )
   1.120 +		/* Skip to next joystick */ ;
   1.121 +	SDL_joysticks[i] = joystick;
   1.122 +	SDL_Unlock_EventThread();
   1.123 +
   1.124  	return(joystick);
   1.125  }
   1.126