Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Make SDL_SetError and friends unconditionally return -1.

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 8163efd819129d1a40afcfee5696c42e41252b35
Showing with 616 additions and 1,189 deletions.
  1. +4 −2 include/SDL_error.h
  2. +6 −12 src/SDL.c
  3. +11 −15 src/SDL_error.c
  4. +5 −10 src/audio/SDL_audiocvt.c
  5. +4 −8 src/audio/SDL_wave.c
  6. +5 −8 src/audio/directsound/SDL_directsound.c
  7. +2 −4 src/audio/sun/SDL_sunaudio.c
  8. +4 −8 src/core/android/SDL_android.cpp
  9. +2 −1 src/core/windows/SDL_windows.c
  10. +2 −2 src/core/windows/SDL_windows.h
  11. +1 −2 src/events/SDL_events.c
  12. +3 −6 src/events/SDL_gesture.c
  13. +1 −2 src/events/SDL_mouse.c
  14. +4 −8 src/events/SDL_touch.c
  15. +9 −18 src/file/SDL_rwops.c
  16. +11 −22 src/haptic/SDL_haptic.c
  17. +30 −56 src/haptic/darwin/SDL_syshaptic.c
  18. +21 −36 src/haptic/linux/SDL_syshaptic.c
  19. +31 −58 src/haptic/windows/SDL_syshaptic.c
  20. +1 −2 src/joystick/SDL_gamecontroller.c
  21. +1 −2 src/joystick/SDL_joystick.c
  22. +7 −12 src/joystick/android/SDL_androidjoystick.c
  23. +3 −6 src/joystick/beos/SDL_bejoystick.cc
  24. +5 −9 src/joystick/bsd/SDL_sysjoystick.c
  25. +5 −9 src/joystick/darwin/SDL_sysjoystick.c
  26. +1 −2 src/joystick/dummy/SDL_sysjoystick.c
  27. +0 −3 src/joystick/iphoneos/SDL_sysjoystick.m
  28. +6 −12 src/joystick/linux/SDL_sysjoystick.c
  29. +2 −4 src/joystick/psp/SDL_sysjoystick.c
  30. +22 −45 src/joystick/windows/SDL_dxjoystick.c
  31. +1 −2 src/joystick/windows/SDL_mmjoystick.c
  32. +1 −2 src/main/beos/SDL_BeApp.cc
  33. +24 −47 src/render/SDL_render.c
  34. +4 −7 src/render/SDL_yuv_sw.c
  35. +30 −58 src/render/direct3d/SDL_render_d3d.c
  36. +7 −13 src/render/opengl/SDL_render_gl.c
  37. +14 −26 src/render/opengles/SDL_render_gles.c
  38. +30 −59 src/render/opengles2/SDL_render_gles2.c
  39. +1 −2 src/render/psp/SDL_render_psp.c
  40. +6 −12 src/render/software/SDL_blendfillrect.c
  41. +4 −8 src/render/software/SDL_blendline.c
  42. +6 −12 src/render/software/SDL_blendpoint.c
  43. +4 −8 src/render/software/SDL_drawline.c
  44. +6 −12 src/render/software/SDL_drawpoint.c
  45. +5 −10 src/render/software/SDL_render_sw.c
  46. +2 −2 src/test/SDL_test_fuzzer.c
  47. +4 −8 src/thread/beos/SDL_syssem.c
  48. +1 −2 src/thread/beos/SDL_systhread.c
  49. +3 −6 src/thread/generic/SDL_syscond.c
  50. +4 −8 src/thread/generic/SDL_sysmutex.c
  51. +7 −15 src/thread/generic/SDL_syssem.c
  52. +1 −2 src/thread/generic/SDL_systhread.c
  53. +3 −6 src/thread/psp/SDL_syscond.c
  54. +3 −6 src/thread/psp/SDL_sysmutex.c
  55. +3 −6 src/thread/psp/SDL_syssem.c
  56. +1 −2 src/thread/psp/SDL_systhread.c
  57. +10 −24 src/thread/pthread/SDL_syscond.c
  58. +11 −25 src/thread/pthread/SDL_sysmutex.c
  59. +5 −9 src/thread/pthread/SDL_syssem.c
  60. +6 −11 src/thread/pthread/SDL_systhread.c
  61. +3 −6 src/thread/windows/SDL_sysmutex.c
  62. +4 −8 src/thread/windows/SDL_syssem.c
  63. +4 −7 src/thread/windows/SDL_systhread.c
  64. +2 −4 src/video/SDL_RLEaccel.c
  65. +2 −3 src/video/SDL_blit.c
  66. +4 −8 src/video/SDL_fillrect.c
  67. +2 −4 src/video/SDL_pixels.c
  68. +8 −16 src/video/SDL_stretch.c
  69. +9 −18 src/video/SDL_surface.c
  70. +40 −83 src/video/SDL_video.c
  71. +1 −2 src/video/android/SDL_androidgl.c
  72. +1 −2 src/video/android/SDL_androidwindow.c
  73. +1 −2 src/video/bwindow/SDL_bframebuffer.cc
  74. +1 −2 src/video/bwindow/SDL_bmodes.cc
  75. +2 −2 src/video/cocoa/SDL_cocoamodes.m
  76. +1 −2 src/video/cocoa/SDL_cocoamouse.m
  77. +1 −2 src/video/cocoa/SDL_cocoaopengl.m
  78. +3 −6 src/video/cocoa/SDL_cocoawindow.m
  79. +3 −6 src/video/directfb/SDL_DirectFB_opengl.c
  80. +5 −10 src/video/directfb/SDL_DirectFB_render.c
  81. +3 −3 src/video/directfb/SDL_DirectFB_window.c
  82. +1 −2 src/video/dummy/SDL_nullframebuffer.c
  83. +12 −24 src/video/pandora/SDL_pandora.c
  84. +2 −4 src/video/psp/SDL_pspgl.c
  85. +1 −2 src/video/psp/SDL_pspvideo.c
  86. +2 −4 src/video/uikit/SDL_uikitmodes.m
  87. +1 −2 src/video/uikit/SDL_uikitopengles.m
  88. +1 −1 src/video/uikit/SDL_uikitvideo.m
  89. +3 −6 src/video/uikit/SDL_uikitwindow.m
  90. +2 −4 src/video/windows/SDL_windowsclipboard.c
  91. +2 −3 src/video/windows/SDL_windowsevents.c
  92. +1 −2 src/video/windows/SDL_windowsframebuffer.c
  93. +2 −4 src/video/windows/SDL_windowsmodes.c
  94. +1 −2 src/video/windows/SDL_windowsmouse.c
  95. +13 −26 src/video/windows/SDL_windowsopengl.c
  96. +1 −1 src/video/windows/SDL_windowsvideo.c
  97. +5 −10 src/video/windows/SDL_windowswindow.c
  98. +1 −2 src/video/x11/SDL_x11clipboard.c
  99. +5 −10 src/video/x11/SDL_x11framebuffer.c
  100. +8 −16 src/video/x11/SDL_x11messagebox.c
  101. +5 −10 src/video/x11/SDL_x11modes.c
  102. +7 −15 src/video/x11/SDL_x11opengl.c
  103. +14 −30 src/video/x11/SDL_x11opengles.c
  104. +1 −2 src/video/x11/SDL_x11shape.c
  105. +1 −1 src/video/x11/SDL_x11video.c
  106. +8 −16 src/video/x11/SDL_x11window.c
@@ -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);

@@ -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++ */
@@ -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
}

@@ -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
}

@@ -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
}

@@ -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
}

@@ -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
}

@@ -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
}

@@ -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();
@@ -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
@@ -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");
}
}

@@ -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",
@@ -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;

@@ -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;

@@ -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);
}
@@ -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;
@@ -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);
}


@@ -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);

@@ -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 {
@@ -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);

@@ -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) {
@@ -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;

@@ -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;
@@ -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];
@@ -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
@@ -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);
@@ -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);
}
@@ -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
@@ -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;

@@ -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;
@@ -413,8 +413,7 @@ SDL_SetRelativeMouseMode(SDL_bool enabled)
}

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

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

0 comments on commit 8163efd

Please sign in to comment.