changeset 4147:6bdec986f2f6 SDL-1.2

Fix bug 545, by returning early whenever an error occurs when initializing joystick structure
author Patrice Mandin <patmandin@gmail.com>
date Sat, 19 Jul 2008 11:54:55 +0000
parents dc726b233f5f
children 3e7f2edce658
files src/joystick/SDL_joystick.c
diffstat 1 files changed, 59 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/src/joystick/SDL_joystick.c	Sat Jul 05 18:27:27 2008 +0000
+++ b/src/joystick/SDL_joystick.c	Sat Jul 19 11:54:55 2008 +0000
@@ -112,64 +112,67 @@
 
 	/* Create and initialize the joystick */
 	joystick = (SDL_Joystick *)SDL_malloc((sizeof *joystick));
-	if ( joystick != NULL ) {
-		SDL_memset(joystick, 0, (sizeof *joystick));
-		joystick->index = device_index;
-		if ( SDL_SYS_JoystickOpen(joystick) < 0 ) {
-			SDL_free(joystick);
-			joystick = NULL;
-		} else {
-			if ( joystick->naxes > 0 ) {
-				joystick->axes = (Sint16 *)SDL_malloc
-					(joystick->naxes*sizeof(Sint16));
-			}
-			if ( joystick->nhats > 0 ) {
-				joystick->hats = (Uint8 *)SDL_malloc
-					(joystick->nhats*sizeof(Uint8));
-			}
-			if ( joystick->nballs > 0 ) {
-				joystick->balls = (struct balldelta *)SDL_malloc
-				  (joystick->nballs*sizeof(*joystick->balls));
-			}
-			if ( joystick->nbuttons > 0 ) {
-				joystick->buttons = (Uint8 *)SDL_malloc
-					(joystick->nbuttons*sizeof(Uint8));
-			}
-			if ( ((joystick->naxes > 0) && !joystick->axes)
-			  || ((joystick->nhats > 0) && !joystick->hats)
-			  || ((joystick->nballs > 0) && !joystick->balls)
-			  || ((joystick->nbuttons > 0) && !joystick->buttons)) {
-				SDL_OutOfMemory();
-				SDL_JoystickClose(joystick);
-				joystick = NULL;
-			}
-			if ( joystick->axes ) {
-				SDL_memset(joystick->axes, 0,
-					joystick->naxes*sizeof(Sint16));
-			}
-			if ( joystick->hats ) {
-				SDL_memset(joystick->hats, 0,
-					joystick->nhats*sizeof(Uint8));
-			}
-			if ( joystick->balls ) {
-				SDL_memset(joystick->balls, 0,
-				  joystick->nballs*sizeof(*joystick->balls));
-			}
-			if ( joystick->buttons ) {
-				SDL_memset(joystick->buttons, 0,
-					joystick->nbuttons*sizeof(Uint8));
-			}
-		}
+	if ( !joystick ) {
+		return(NULL);
+	}
+
+	SDL_memset(joystick, 0, (sizeof *joystick));
+	joystick->index = device_index;
+	if ( SDL_SYS_JoystickOpen(joystick) < 0 ) {
+		SDL_free(joystick);
+		return(NULL);
+	}
+
+	if ( joystick->naxes > 0 ) {
+		joystick->axes = (Sint16 *)SDL_malloc
+			(joystick->naxes*sizeof(Sint16));
+	}
+	if ( joystick->nhats > 0 ) {
+		joystick->hats = (Uint8 *)SDL_malloc
+			(joystick->nhats*sizeof(Uint8));
+	}
+	if ( joystick->nballs > 0 ) {
+		joystick->balls = (struct balldelta *)SDL_malloc
+			(joystick->nballs*sizeof(*joystick->balls));
+	}
+	if ( joystick->nbuttons > 0 ) {
+		joystick->buttons = (Uint8 *)SDL_malloc
+			(joystick->nbuttons*sizeof(Uint8));
 	}
-	if ( joystick ) {
-		/* Add joystick to list */
-		++joystick->ref_count;
-		SDL_Lock_EventThread();
-		for ( i=0; SDL_joysticks[i]; ++i )
-			/* Skip to next joystick */;
-		SDL_joysticks[i] = joystick;
-		SDL_Unlock_EventThread();
+	if ( ((joystick->naxes > 0) && !joystick->axes)
+	  || ((joystick->nhats > 0) && !joystick->hats)
+	  || ((joystick->nballs > 0) && !joystick->balls)
+	  || ((joystick->nbuttons > 0) && !joystick->buttons)) {
+		SDL_OutOfMemory();
+		SDL_JoystickClose(joystick);
+		return(NULL);
+	}
+
+	if ( joystick->axes ) {
+		SDL_memset(joystick->axes, 0,
+			joystick->naxes*sizeof(Sint16));
+	}
+	if ( joystick->hats ) {
+		SDL_memset(joystick->hats, 0,
+			joystick->nhats*sizeof(Uint8));
 	}
+	if ( joystick->balls ) {
+		SDL_memset(joystick->balls, 0,
+			joystick->nballs*sizeof(*joystick->balls));
+	}
+	if ( joystick->buttons ) {
+		SDL_memset(joystick->buttons, 0,
+			joystick->nbuttons*sizeof(Uint8));
+	}
+
+	/* Add joystick to list */
+	++joystick->ref_count;
+	SDL_Lock_EventThread();
+	for ( i=0; SDL_joysticks[i]; ++i )
+		/* Skip to next joystick */ ;
+	SDL_joysticks[i] = joystick;
+	SDL_Unlock_EventThread();
+
 	return(joystick);
 }