Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Make SDL_SetError and friends unconditionally return -1.
Browse files Browse the repository at this point in the history
This lets us change things like this...

    if (Failed) {
        SDL_SetError("We failed");
        return -1;
    }

...into this...

    if (Failed) {
        return SDL_SetError("We failed");
    }


 Fixes Bugzilla #1778.
  • Loading branch information
icculus committed Mar 31, 2013
1 parent a979ef0 commit 8163efd
Show file tree
Hide file tree
Showing 106 changed files with 616 additions and 1,189 deletions.
6 changes: 4 additions & 2 deletions include/SDL_error.h
Expand Up @@ -39,7 +39,8 @@ extern "C" {
#endif

/* Public functions */
extern DECLSPEC void SDLCALL SDL_SetError(const char *fmt, ...);
/* SDL_SetError() unconditionally returns -1. */
extern DECLSPEC int SDLCALL SDL_SetError(const char *fmt, ...);
extern DECLSPEC const char *SDLCALL SDL_GetError(void);
extern DECLSPEC void SDLCALL SDL_ClearError(void);

Expand All @@ -62,7 +63,8 @@ typedef enum
SDL_UNSUPPORTED,
SDL_LASTERROR
} SDL_errorcode;
extern DECLSPEC void SDLCALL SDL_Error(SDL_errorcode code);
/* SDL_Error() unconditionally returns -1. */
extern DECLSPEC int SDLCALL SDL_Error(SDL_errorcode code);
/*@}*//*Internal error functions*/

/* Ends C function definitions when using C++ */
Expand Down
18 changes: 6 additions & 12 deletions src/SDL.c
Expand Up @@ -109,8 +109,7 @@ SDL_InitSubSystem(Uint32 flags)
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_TIMER);
#else
SDL_SetError("SDL not built with timer support");
return (-1);
return SDL_SetError("SDL not built with timer support");
#endif
}

Expand All @@ -124,8 +123,7 @@ SDL_InitSubSystem(Uint32 flags)
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_VIDEO);
#else
SDL_SetError("SDL not built with video support");
return (-1);
return SDL_SetError("SDL not built with video support");
#endif
}

Expand All @@ -139,8 +137,7 @@ SDL_InitSubSystem(Uint32 flags)
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_AUDIO);
#else
SDL_SetError("SDL not built with audio support");
return (-1);
return SDL_SetError("SDL not built with audio support");
#endif
}

Expand All @@ -159,8 +156,7 @@ SDL_InitSubSystem(Uint32 flags)
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_JOYSTICK);
#else
SDL_SetError("SDL not built with joystick support");
return (-1);
return SDL_SetError("SDL not built with joystick support");
#endif
}

Expand All @@ -173,8 +169,7 @@ SDL_InitSubSystem(Uint32 flags)
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_GAMECONTROLLER);
#else
SDL_SetError("SDL not built with joystick support");
return (-1);
return SDL_SetError("SDL not built with joystick support");
#endif
}

Expand All @@ -188,8 +183,7 @@ SDL_InitSubSystem(Uint32 flags)
}
SDL_PrivateSubsystemRefCountIncr(SDL_INIT_HAPTIC);
#else
SDL_SetError("SDL not built with haptic (force feedback) support");
return (-1);
return SDL_SetError("SDL not built with haptic (force feedback) support");
#endif
}

Expand Down
26 changes: 11 additions & 15 deletions src/SDL_error.c
Expand Up @@ -49,14 +49,14 @@ SDL_LookupString(const char *key)

/* Public functions */

void
int
SDL_SetError(const char *fmt, ...)
{
va_list ap;
SDL_error *error;

/* Ignore call if invalid format pointer was passed */
if (fmt == NULL) return;
if (fmt == NULL) return -1;

/* Copy in the key, mark error as valid */
error = SDL_GetErrBuf();
Expand Down Expand Up @@ -112,6 +112,8 @@ SDL_SetError(const char *fmt, ...)

/* If we are in debug mode, print out an error message */
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", SDL_GetError());

return -1;
}

/* This function has a bit more overhead than most error functions
Expand Down Expand Up @@ -216,28 +218,22 @@ SDL_ClearError(void)
}

/* Very common errors go here */
void
int
SDL_Error(SDL_errorcode code)
{
switch (code) {
case SDL_ENOMEM:
SDL_SetError("Out of memory");
break;
return SDL_SetError("Out of memory");
case SDL_EFREAD:
SDL_SetError("Error reading from datastream");
break;
return SDL_SetError("Error reading from datastream");
case SDL_EFWRITE:
SDL_SetError("Error writing to datastream");
break;
return SDL_SetError("Error writing to datastream");
case SDL_EFSEEK:
SDL_SetError("Error seeking in datastream");
break;
return SDL_SetError("Error seeking in datastream");
case SDL_UNSUPPORTED:
SDL_SetError("That operation is not supported");
break;
return SDL_SetError("That operation is not supported");
default:
SDL_SetError("Unknown SDL error");
break;
return SDL_SetError("Unknown SDL error");
}
}

Expand Down
15 changes: 5 additions & 10 deletions src/audio/SDL_audiocvt.c
Expand Up @@ -970,28 +970,23 @@ SDL_BuildAudioCVT(SDL_AudioCVT * cvt,

/* Sanity check target pointer */
if (cvt == NULL) {
SDL_InvalidParamError("cvt");
return -1;
return SDL_InvalidParamError("cvt");
}

/* there are no unsigned types over 16 bits, so catch this up front. */
if ((SDL_AUDIO_BITSIZE(src_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(src_fmt))) {
SDL_SetError("Invalid source format");
return -1;
return SDL_SetError("Invalid source format");
}
if ((SDL_AUDIO_BITSIZE(dst_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(dst_fmt))) {
SDL_SetError("Invalid destination format");
return -1;
return SDL_SetError("Invalid destination format");
}

/* prevent possible divisions by zero, etc. */
if ((src_channels == 0) || (dst_channels == 0)) {
SDL_SetError("Source or destination channels is zero");
return -1;
return SDL_SetError("Source or destination channels is zero");
}
if ((src_rate == 0) || (dst_rate == 0)) {
SDL_SetError("Source or destination rate is zero");
return -1;
return SDL_SetError("Source or destination rate is zero");
}
#ifdef DEBUG_CONVERT
printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
Expand Down
12 changes: 4 additions & 8 deletions src/audio/SDL_wave.c
Expand Up @@ -134,8 +134,7 @@ MS_ADPCM_decode(Uint8 ** audio_buf, Uint32 * audio_len)
MS_ADPCM_state.wavefmt.channels * sizeof(Sint16);
*audio_buf = (Uint8 *) SDL_malloc(*audio_len);
if (*audio_buf == NULL) {
SDL_Error(SDL_ENOMEM);
return (-1);
return SDL_OutOfMemory();
}
decoded = *audio_buf;

Expand Down Expand Up @@ -359,8 +358,7 @@ IMA_ADPCM_decode(Uint8 ** audio_buf, Uint32 * audio_len)
IMA_ADPCM_state.wavefmt.channels * sizeof(Sint16);
*audio_buf = (Uint8 *) SDL_malloc(*audio_len);
if (*audio_buf == NULL) {
SDL_Error(SDL_ENOMEM);
return (-1);
return SDL_OutOfMemory();
}
decoded = *audio_buf;

Expand Down Expand Up @@ -620,14 +618,12 @@ ReadChunk(SDL_RWops * src, Chunk * chunk)
chunk->length = SDL_ReadLE32(src);
chunk->data = (Uint8 *) SDL_malloc(chunk->length);
if (chunk->data == NULL) {
SDL_Error(SDL_ENOMEM);
return (-1);
return SDL_OutOfMemory();
}
if (SDL_RWread(src, chunk->data, chunk->length, 1) != 1) {
SDL_Error(SDL_EFREAD);
SDL_free(chunk->data);
chunk->data = NULL;
return (-1);
return SDL_Error(SDL_EFREAD);
}
return (chunk->length);
}
Expand Down
13 changes: 5 additions & 8 deletions src/audio/directsound/SDL_directsound.c
Expand Up @@ -95,7 +95,7 @@ utf16_to_utf8(const WCHAR *S)
(SDL_wcslen(S)+1)*sizeof(WCHAR));
}

static void
static int
SetDSerror(const char *function, int code)
{
static const char *error;
Expand Down Expand Up @@ -145,8 +145,7 @@ SetDSerror(const char *function, int code)
SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function,
error);
}
SDL_SetError("%s", errbuf);
return;
return SDL_SetError("%s", errbuf);
}


Expand Down Expand Up @@ -380,16 +379,14 @@ CreateSecondary(_THIS, HWND focus, WAVEFORMATEX * wavefmt)
format.dwBufferBytes = numchunks * chunksize;
if ((format.dwBufferBytes < DSBSIZE_MIN) ||
(format.dwBufferBytes > DSBSIZE_MAX)) {
SDL_SetError("Sound buffer size must be between %d and %d",
DSBSIZE_MIN / numchunks, DSBSIZE_MAX / numchunks);
return (-1);
return SDL_SetError("Sound buffer size must be between %d and %d",
DSBSIZE_MIN / numchunks, DSBSIZE_MAX / numchunks);
}
format.dwReserved = 0;
format.lpwfxFormat = wavefmt;
result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL);
if (result != DS_OK) {
SetDSerror("DirectSound CreateSoundBuffer", result);
return (-1);
return SetDSerror("DirectSound CreateSoundBuffer", result);
}
IDirectSoundBuffer_SetFormat(*sndbuf, wavefmt);

Expand Down
6 changes: 2 additions & 4 deletions src/audio/sun/SDL_sunaudio.c
Expand Up @@ -332,8 +332,7 @@ SUNAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
this->hidden->frequency = 8;
this->hidden->ulaw_buf = (Uint8 *) SDL_malloc(this->hidden->fragsize);
if (this->hidden->ulaw_buf == NULL) {
SDL_OutOfMemory();
return (-1);
return SDL_OutOfMemory();
}
this->spec.channels = 1;
} else {
Expand All @@ -353,8 +352,7 @@ SUNAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
/* Allocate mixing buffer */
this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->spec.size);
if (this->hidden->mixbuf == NULL) {
SDL_OutOfMemory();
return (-1);
return SDL_OutOfMemory();
}
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);

Expand Down
12 changes: 4 additions & 8 deletions src/core/android/SDL_android.cpp
Expand Up @@ -811,8 +811,7 @@ static int Android_JNI_FileClose(SDL_RWops* ctx, bool release)
JNIEnv *mEnv = Android_JNI_GetEnv();

if (!refs.init(mEnv)) {
SDL_SetError("Failed to allocate enough JVM local references");
return -1;
return SDL_SetError("Failed to allocate enough JVM local references");
}

if (ctx) {
Expand Down Expand Up @@ -875,8 +874,7 @@ extern "C" Sint64 Android_JNI_FileSeek(SDL_RWops* ctx, Sint64 offset, int whence
offset = ctx->hidden.androidio.offset + ctx->hidden.androidio.size + offset;
break;
default:
SDL_SetError("Unknown value for 'whence'");
return -1;
return SDL_SetError("Unknown value for 'whence'");
}
whence = SEEK_SET;

Expand All @@ -897,14 +895,12 @@ extern "C" Sint64 Android_JNI_FileSeek(SDL_RWops* ctx, Sint64 offset, int whence
newPosition = ctx->hidden.androidio.size + offset;
break;
default:
SDL_SetError("Unknown value for 'whence'");
return -1;
return SDL_SetError("Unknown value for 'whence'");
}

/* Validate the new position */
if (newPosition < 0) {
SDL_Error(SDL_EFSEEK);
return -1;
return SDL_Error(SDL_EFSEEK);
}
if (newPosition > ctx->hidden.androidio.size) {
newPosition = ctx->hidden.androidio.size;
Expand Down
3 changes: 2 additions & 1 deletion src/core/windows/SDL_windows.c
Expand Up @@ -29,7 +29,7 @@
#include <objbase.h> /* for CoInitialize/CoUninitialize */

/* Sets an error message based on GetLastError() */
void
int
WIN_SetError(const char *prefix)
{
TCHAR buffer[1024];
Expand All @@ -39,6 +39,7 @@ WIN_SetError(const char *prefix)
message = WIN_StringToUTF8(buffer);
SDL_SetError("%s%s%s", prefix ? prefix : "", prefix ? ": " : "", message);
SDL_free(message);
return -1;
}

HRESULT
Expand Down
4 changes: 2 additions & 2 deletions src/core/windows/SDL_windows.h
Expand Up @@ -43,8 +43,8 @@
#define WIN_UTF8ToString(S) SDL_iconv_string("ASCII", "UTF-8", (char *)(S), SDL_strlen(S)+1)
#endif

/* Sets an error message based on GetLastError() */
extern void WIN_SetError(const char *prefix);
/* Sets an error message based on GetLastError(). Always return -1. */
extern int WIN_SetError(const char *prefix);

/* Wrap up the oddities of CoInitialize() into a common function. */
extern HRESULT WIN_CoInitialize(void);
Expand Down
3 changes: 1 addition & 2 deletions src/events/SDL_events.c
Expand Up @@ -244,8 +244,7 @@ SDL_PeepEvents(SDL_Event * events, int numevents, SDL_eventaction action,
}
SDL_UnlockMutex(SDL_EventQ.lock);
} else {
SDL_SetError("Couldn't lock event queue");
used = -1;
return SDL_SetError("Couldn't lock event queue");
}
return (used);
}
Expand Down
9 changes: 3 additions & 6 deletions src/events/SDL_gesture.c
Expand Up @@ -158,8 +158,7 @@ int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src)
}
}
}
SDL_SetError("Unknown gestureId");
return -1;
return SDL_SetError("Unknown gestureId");
}

//path is an already sampled set of points
Expand All @@ -176,8 +175,7 @@ static int SDL_AddDollarGesture_one(SDL_GestureTouch* inTouch, SDL_FloatPoint* p
(index + 1) *
sizeof(SDL_DollarTemplate));
if (!dollarTemplate) {
SDL_OutOfMemory();
return -1;
return SDL_OutOfMemory();
}
inTouch->dollarTemplate = dollarTemplate;

Expand Down Expand Up @@ -419,8 +417,7 @@ int SDL_GestureAddTouch(SDL_TouchID touchId)
sizeof(SDL_GestureTouch));

if (!gestureTouch) {
SDL_OutOfMemory();
return -1;
return SDL_OutOfMemory();
}

SDL_gestureTouch = gestureTouch;
Expand Down
3 changes: 1 addition & 2 deletions src/events/SDL_mouse.c
Expand Up @@ -413,8 +413,7 @@ SDL_SetRelativeMouseMode(SDL_bool enabled)
}

if (!mouse->SetRelativeMouseMode) {
SDL_Unsupported();
return -1;
return SDL_Unsupported();
}

if (mouse->SetRelativeMouseMode(enabled) < 0) {
Expand Down

0 comments on commit 8163efd

Please sign in to comment.