SDL: error: SDL's allocators now call SDL_OutOfMemory on error.

From 447b508a77b8601d23ca48cf189fe4436ab33572 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Thu, 30 Nov 2023 00:14:27 -0500
Subject: [PATCH] error: SDL's allocators now call SDL_OutOfMemory on error.

This means the allocator's caller doesn't need to use SDL_OutOfMemory directly
if the allocation fails.

This applies to the usual allocators: SDL_malloc, SDL_calloc, SDL_realloc
(all of these regardless of if the app supplied a custom allocator or we're
using system malloc() or an internal copy of dlmalloc under the hood),
SDL_aligned_alloc, SDL_small_alloc, SDL_strdup, SDL_asprintf, SDL_wcsdup...
probably others. If it returns something you can pass to SDL_free, it should
work.

The caller might still need to use SDL_OutOfMemory if something that wasn't
SDL allocated the memory: operator new in C++ code, Objective-C's alloc
message, win32 GlobalAlloc, etc.

Fixes #8642.
---
 src/SDL_hashtable.c                           |  3 --
 src/SDL_hints.c                               |  6 +--
 src/SDL_list.c                                |  2 +-
 src/SDL_properties.c                          | 18 +++-----
 src/audio/SDL_audio.c                         | 16 +++----
 src/audio/SDL_audiocvt.c                      |  8 +---
 src/audio/SDL_audioqueue.c                    |  8 ++--
 src/audio/SDL_wave.c                          | 30 ++++++-------
 src/audio/aaudio/SDL_aaudio.c                 |  4 +-
 src/audio/alsa/SDL_alsa_audio.c               |  4 +-
 src/audio/android/SDL_androidaudio.c          |  2 +-
 src/audio/coreaudio/SDL_coreaudio.m           |  4 +-
 src/audio/directsound/SDL_directsound.c       |  2 +-
 src/audio/disk/SDL_diskaudio.c                |  4 +-
 src/audio/dsp/SDL_dspaudio.c                  |  4 +-
 src/audio/dummy/SDL_dummyaudio.c              |  4 +-
 src/audio/emscripten/SDL_emscriptenaudio.c    |  4 +-
 src/audio/jack/SDL_jackaudio.c                |  6 +--
 src/audio/n3ds/SDL_n3dsaudio.c                |  4 +-
 src/audio/netbsd/SDL_netbsdaudio.c            |  4 +-
 src/audio/openslES/SDL_openslES.c             |  2 +-
 src/audio/pipewire/SDL_pipewire.c             |  3 +-
 src/audio/ps2/SDL_ps2audio.c                  |  2 +-
 src/audio/psp/SDL_pspaudio.c                  |  2 +-
 src/audio/pulseaudio/SDL_pulseaudio.c         |  4 +-
 src/audio/qnx/SDL_qsa_audio.c                 |  4 +-
 src/audio/sndio/SDL_sndioaudio.c              |  6 +--
 src/audio/vita/SDL_vitaaudio.c                |  5 +--
 src/audio/wasapi/SDL_wasapi.c                 |  4 +-
 src/core/linux/SDL_evdev.c                    | 12 +++---
 src/core/linux/SDL_udev.c                     |  4 +-
 src/core/windows/SDL_immdevice.c              |  2 -
 src/events/SDL_events.c                       |  3 --
 src/events/SDL_mouse.c                        |  5 +--
 src/events/SDL_touch.c                        | 12 +++---
 src/file/SDL_rwops.c                          |  9 +---
 src/file/n3ds/SDL_rwopsromfs.c                |  1 -
 src/filesystem/android/SDL_sysfilesystem.c    |  1 -
 src/filesystem/cocoa/SDL_sysfilesystem.m      | 17 ++------
 src/filesystem/emscripten/SDL_sysfilesystem.c | 10 +----
 src/filesystem/gdk/SDL_sysfilesystem.cpp      |  1 -
 src/filesystem/haiku/SDL_sysfilesystem.cc     | 21 +++------
 src/filesystem/n3ds/SDL_sysfilesystem.c       |  1 -
 src/filesystem/riscos/SDL_sysfilesystem.c     |  3 --
 src/filesystem/unix/SDL_sysfilesystem.c       | 15 +------
 src/filesystem/vita/SDL_sysfilesystem.c       |  1 -
 src/filesystem/windows/SDL_sysfilesystem.c    |  3 --
 src/filesystem/winrt/SDL_sysfilesystem.cpp    |  3 --
 src/haptic/SDL_haptic.c                       |  2 -
 src/haptic/android/SDL_syshaptic.c            |  1 -
 src/haptic/darwin/SDL_syshaptic.c             | 43 +++++++------------
 src/haptic/linux/SDL_syshaptic.c              |  9 ++--
 src/haptic/windows/SDL_dinputhaptic.c         | 42 ++++++++----------
 src/haptic/windows/SDL_windowshaptic.c        |  5 +--
 src/haptic/windows/SDL_xinputhaptic.c         | 13 +++---
 src/joystick/SDL_gamepad.c                    | 27 ++----------
 src/joystick/SDL_joystick.c                   |  5 ---
 src/joystick/apple/SDL_mfijoystick.m          |  2 -
 src/joystick/bsd/SDL_bsdjoystick.c            |  3 +-
 src/joystick/darwin/SDL_iokitjoystick.c       |  3 +-
 src/joystick/haiku/SDL_haikujoystick.cc       | 14 +++---
 src/joystick/hidapi/SDL_hidapi_gamecube.c     |  1 -
 src/joystick/hidapi/SDL_hidapi_luna.c         |  1 -
 src/joystick/hidapi/SDL_hidapi_ps3.c          |  2 -
 src/joystick/hidapi/SDL_hidapi_ps4.c          |  1 -
 src/joystick/hidapi/SDL_hidapi_ps5.c          |  1 -
 src/joystick/hidapi/SDL_hidapi_rumble.c       |  2 +-
 src/joystick/hidapi/SDL_hidapi_shield.c       |  1 -
 src/joystick/hidapi/SDL_hidapi_stadia.c       |  1 -
 src/joystick/hidapi/SDL_hidapi_steam.c        |  1 -
 src/joystick/hidapi/SDL_hidapi_switch.c       |  1 -
 src/joystick/hidapi/SDL_hidapi_wii.c          |  1 -
 src/joystick/hidapi/SDL_hidapi_xbox360.c      |  1 -
 src/joystick/hidapi/SDL_hidapi_xbox360w.c     |  1 -
 src/joystick/hidapi/SDL_hidapi_xboxone.c      |  1 -
 src/joystick/hidapi/SDL_hidapijoystick.c      |  2 +-
 src/joystick/linux/SDL_sysjoystick.c          |  6 +--
 src/joystick/virtual/SDL_virtualjoystick.c    |  8 ++--
 src/joystick/windows/SDL_dinputjoystick.c     |  2 +-
 src/joystick/windows/SDL_rawinputjoystick.c   | 14 +++---
 .../windows/SDL_windows_gaming_input.c        |  3 +-
 src/joystick/windows/SDL_windowsjoystick.c    |  6 +--
 src/locale/SDL_locale.c                       |  1 -
 src/locale/unix/SDL_syslocale.c               |  2 +-
 src/locale/windows/SDL_syslocale.c            |  2 +-
 src/misc/windows/SDL_sysurl.c                 |  2 +-
 src/misc/winrt/SDL_sysurl.cpp                 |  2 +-
 src/render/SDL_render.c                       | 19 ++++----
 src/render/SDL_yuv_sw.c                       |  3 --
 src/render/direct3d/SDL_render_d3d.c          |  6 +--
 src/render/direct3d11/SDL_render_d3d11.c      |  6 +--
 src/render/direct3d12/SDL_render_d3d12.c      |  6 +--
 src/render/metal/SDL_render_metal.m           |  5 +--
 src/render/opengl/SDL_render_gl.c             |  8 ++--
 src/render/opengles2/SDL_render_gles2.c       | 11 ++---
 src/render/ps2/SDL_render_ps2.c               |  6 +--
 src/render/psp/SDL_render_psp.c               | 12 +++---
 src/render/software/SDL_render_sw.c           |  2 -
 src/render/vitagxm/SDL_render_vita_gxm.c      |  6 +--
 src/sensor/SDL_sensor.c                       |  3 --
 src/sensor/android/SDL_androidsensor.c        |  2 +-
 src/sensor/coremotion/SDL_coremotionsensor.m  |  4 +-
 src/sensor/vita/SDL_vitasensor.c              |  4 +-
 src/sensor/windows/SDL_windowssensor.c        |  4 +-
 src/stdlib/SDL_malloc.c                       |  9 ++++
 src/test/SDL_test_common.c                    |  1 -
 src/thread/SDL_thread.c                       |  9 +---
 src/thread/generic/SDL_syscond.c              |  2 -
 src/thread/generic/SDL_sysmutex.c             |  2 -
 src/thread/generic/SDL_sysrwlock.c            |  1 -
 src/thread/generic/SDL_syssem.c               |  1 -
 src/thread/n3ds/SDL_syscond.c                 |  2 -
 src/thread/n3ds/SDL_sysmutex.c                |  2 -
 src/thread/n3ds/SDL_syssem.c                  |  1 -
 src/thread/ps2/SDL_syssem.c                   |  2 -
 src/thread/psp/SDL_syscond.c                  |  2 -
 src/thread/psp/SDL_sysmutex.c                 |  2 -
 src/thread/psp/SDL_syssem.c                   |  2 -
 src/thread/pthread/SDL_sysmutex.c             |  2 -
 src/thread/pthread/SDL_sysrwlock.c            |  2 -
 src/thread/pthread/SDL_syssem.c               |  2 -
 src/thread/vita/SDL_syscond.c                 |  2 -
 src/thread/vita/SDL_sysmutex.c                |  2 -
 src/thread/vita/SDL_syssem.c                  |  2 -
 src/thread/windows/SDL_syscond_cv.c           | 15 ++-----
 src/thread/windows/SDL_sysmutex.c             | 13 ++----
 src/thread/windows/SDL_sysrwlock_srw.c        |  5 +--
 src/thread/windows/SDL_syssem.c               |  9 +---
 src/timer/SDL_timer.c                         |  3 --
 src/video/SDL_RLEaccel.c                      |  4 +-
 src/video/SDL_clipboard.c                     |  4 +-
 src/video/SDL_egl.c                           |  2 +-
 src/video/SDL_pixels.c                        |  6 ---
 src/video/SDL_surface.c                       |  4 --
 src/video/SDL_video.c                         | 11 +----
 src/video/SDL_vulkan_utils.c                  |  6 ---
 src/video/SDL_yuv.c                           | 14 +++---
 src/video/android/SDL_androidmouse.c          |  3 --
 src/video/android/SDL_androidvideo.c          |  2 -
 src/video/android/SDL_androidwindow.c         |  2 +-
 src/video/cocoa/SDL_cocoaclipboard.m          |  2 -
 src/video/cocoa/SDL_cocoamouse.m              |  2 +-
 src/video/cocoa/SDL_cocoavideo.m              |  1 -
 src/video/cocoa/SDL_cocoawindow.m             |  1 -
 src/video/dummy/SDL_nullvideo.c               |  1 -
 src/video/emscripten/SDL_emscriptenmouse.c    |  7 +--
 src/video/emscripten/SDL_emscriptenvideo.c    |  3 +-
 src/video/gdk/SDL_gdktextinput.cpp            | 10 +----
 src/video/haiku/SDL_bvideo.cc                 |  2 -
 src/video/kmsdrm/SDL_kmsdrmmouse.c            |  5 +--
 src/video/kmsdrm/SDL_kmsdrmvideo.c            | 11 ++---
 src/video/n3ds/SDL_n3dsframebuffer.c          |  2 +-
 src/video/n3ds/SDL_n3dsvideo.c                |  5 +--
 src/video/ngage/SDL_ngagevideo.cpp            |  2 -
 src/video/ngage/SDL_ngagewindow.cpp           |  2 +-
 src/video/offscreen/SDL_offscreenvideo.c      |  1 -
 src/video/offscreen/SDL_offscreenwindow.c     |  2 +-
 src/video/ps2/SDL_ps2video.c                  |  1 -
 src/video/psp/SDL_pspvideo.c                  |  5 +--
 src/video/raspberry/SDL_rpimouse.c            |  2 -
 src/video/raspberry/SDL_rpivideo.c            |  4 +-
 src/video/riscos/SDL_riscosframebuffer.c      |  2 +-
 src/video/riscos/SDL_riscosmodes.c            |  6 +--
 src/video/riscos/SDL_riscosmouse.c            |  6 +--
 src/video/riscos/SDL_riscosvideo.c            |  2 -
 src/video/riscos/SDL_riscoswindow.c           |  2 +-
 src/video/uikit/SDL_uikitvideo.m              | 12 +++---
 src/video/vita/SDL_vitavideo.c                |  5 +--
 src/video/vivante/SDL_vivantevideo.c          |  6 +--
 src/video/wayland/SDL_waylanddatamanager.c    | 12 +++---
 src/video/wayland/SDL_waylandevents.c         | 18 ++------
 src/video/wayland/SDL_waylandmessagebox.c     |  2 +-
 src/video/wayland/SDL_waylandmouse.c          | 15 +------
 src/video/wayland/SDL_waylandvideo.c          |  2 -
 src/video/wayland/SDL_waylandwindow.c         |  2 +-
 src/video/windows/SDL_windowsclipboard.c      |  2 -
 src/video/windows/SDL_windowsframebuffer.c    |  2 +-
 src/video/windows/SDL_windowsmessagebox.c     |  1 -
 src/video/windows/SDL_windowsmouse.c          | 10 +----
 src/video/windows/SDL_windowsopengl.c         |  2 +-
 src/video/windows/SDL_windowsvideo.c          |  1 -
 src/video/windows/SDL_windowswindow.c         |  4 +-
 src/video/winrt/SDL_winrtmouse.cpp            |  2 -
 src/video/winrt/SDL_winrtvideo.cpp            |  3 --
 src/video/x11/SDL_x11clipboard.c              |  4 +-
 src/video/x11/SDL_x11framebuffer.c            |  2 +-
 src/video/x11/SDL_x11messagebox.c             |  4 +-
 src/video/x11/SDL_x11modes.c                  |  8 ++--
 src/video/x11/SDL_x11mouse.c                  | 16 +------
 src/video/x11/SDL_x11opengl.c                 |  2 +-
 src/video/x11/SDL_x11video.c                  |  2 -
 src/video/x11/SDL_x11window.c                 |  9 ++--
 src/video/x11/SDL_x11xinput2.c                |  1 -
 test/testffmpeg.c                             |  3 --
 test/testshape.c                              |  2 +-
 test/testutils.c                              | 18 ++------
 test/testvulkan.c                             |  6 ---
 197 files changed, 313 insertions(+), 742 deletions(-)

diff --git a/src/SDL_hashtable.c b/src/SDL_hashtable.c
index 0b898059853f..b6a8b0d49cd3 100644
--- a/src/SDL_hashtable.c
+++ b/src/SDL_hashtable.c
@@ -54,14 +54,12 @@ SDL_HashTable *SDL_CreateHashTable(void *data, const Uint32 num_buckets, const S
 
     table = (SDL_HashTable *) SDL_calloc(1, sizeof (SDL_HashTable));
     if (!table) {
-        SDL_OutOfMemory();
         return NULL;
     }
 
     table->table = (SDL_HashItem **) SDL_calloc(num_buckets, sizeof (SDL_HashItem *));
     if (!table->table) {
         SDL_free(table);
-        SDL_OutOfMemory();
         return NULL;
     }
 
@@ -92,7 +90,6 @@ SDL_bool SDL_InsertIntoHashTable(SDL_HashTable *table, const void *key, const vo
     // !!! FIXME: grow and rehash table if it gets too saturated.
     item = (SDL_HashItem *) SDL_malloc(sizeof (SDL_HashItem));
     if (!item) {
-        SDL_OutOfMemory();
         return SDL_FALSE;
     }
 
diff --git a/src/SDL_hints.c b/src/SDL_hints.c
index 3e792c6d50e1..e45823a4ebb1 100644
--- a/src/SDL_hints.c
+++ b/src/SDL_hints.c
@@ -226,7 +226,7 @@ int SDL_AddHintCallback(const char *name, SDL_HintCallback callback, void *userd
 
     entry = (SDL_HintWatch *)SDL_malloc(sizeof(*entry));
     if (!entry) {
-        return SDL_OutOfMemory();
+        return -1;
     }
     entry->callback = callback;
     entry->userdata = userdata;
@@ -241,13 +241,13 @@ int SDL_AddHintCallback(const char *name, SDL_HintCallback callback, void *userd
         hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
         if (!hint) {
             SDL_free(entry);
-            return SDL_OutOfMemory();
+            return -1;
         }
         hint->name = SDL_strdup(name);
         if (!hint->name) {
             SDL_free(entry);
             SDL_free(hint);
-            return SDL_OutOfMemory();
+            return -1;
         }
         hint->value = NULL;
         hint->priority = SDL_HINT_DEFAULT;
diff --git a/src/SDL_list.c b/src/SDL_list.c
index 64c39245e62a..b6ea10d105c0 100644
--- a/src/SDL_list.c
+++ b/src/SDL_list.c
@@ -28,7 +28,7 @@ int SDL_ListAdd(SDL_ListNode **head, void *ent)
     SDL_ListNode *node = SDL_malloc(sizeof(*node));
 
     if (!node) {
-        return SDL_OutOfMemory();
+        return -1;
     }
 
     node->entry = ent;
diff --git a/src/SDL_properties.c b/src/SDL_properties.c
index 92c260fc1460..b883a4d37dcd 100644
--- a/src/SDL_properties.c
+++ b/src/SDL_properties.c
@@ -271,7 +271,7 @@ int SDL_SetPropertyWithCleanup(SDL_PropertiesID props, const char *name, void *v
 
     property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
     if (!property) {
-        return SDL_OutOfMemory();
+        return -1;
     }
     property->type = SDL_PROPERTY_TYPE_POINTER;
     property->value.pointer_value = value;
@@ -290,7 +290,7 @@ int SDL_SetProperty(SDL_PropertiesID props, const char *name, void *value)
 
     property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
     if (!property) {
-        return SDL_OutOfMemory();
+        return -1;
     }
     property->type = SDL_PROPERTY_TYPE_POINTER;
     property->value.pointer_value = value;
@@ -308,13 +308,13 @@ int SDL_SetStringProperty(SDL_PropertiesID props, const char *name, const char *
 
     property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
     if (!property) {
-        return SDL_OutOfMemory();
+        return -1;
     }
     property->type = SDL_PROPERTY_TYPE_STRING;
     property->value.string_value = SDL_strdup(value);
     if (!property->value.string_value) {
         SDL_free(property);
-        return SDL_OutOfMemory();
+        return -1;
     }
     return SDL_PrivateSetProperty(props, name, property);
 }
@@ -323,7 +323,7 @@ int SDL_SetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 value
 {
     SDL_Property *property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
     if (!property) {
-        return SDL_OutOfMemory();
+        return -1;
     }
     property->type = SDL_PROPERTY_TYPE_NUMBER;
     property->value.number_value = value;
@@ -334,7 +334,7 @@ int SDL_SetFloatProperty(SDL_PropertiesID props, const char *name, float value)
 {
     SDL_Property *property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
     if (!property) {
-        return SDL_OutOfMemory();
+        return -1;
     }
     property->type = SDL_PROPERTY_TYPE_FLOAT;
     property->value.float_value = value;
@@ -345,7 +345,7 @@ int SDL_SetBooleanProperty(SDL_PropertiesID props, const char *name, SDL_bool va
 {
     SDL_Property *property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
     if (!property) {
-        return SDL_OutOfMemory();
+        return -1;
     }
     property->type = SDL_PROPERTY_TYPE_BOOLEAN;
     property->value.boolean_value = value ? SDL_TRUE : SDL_FALSE;
@@ -478,8 +478,6 @@ const char *SDL_GetStringProperty(SDL_PropertiesID props, const char *name, cons
                     SDL_asprintf(&property->string_storage, "%" SDL_PRIs64 "", property->value.number_value);
                     if (property->string_storage) {
                         value = property->string_storage;
-                    } else {
-                        SDL_OutOfMemory();
                     }
                 }
                 break;
@@ -490,8 +488,6 @@ const char *SDL_GetStringProperty(SDL_PropertiesID props, const char *name, cons
                     SDL_asprintf(&property->string_storage, "%f", property->value.float_value);
                     if (property->string_storage) {
                         value = property->string_storage;
-                    } else {
-                        SDL_OutOfMemory();
                     }
                 }
                 break;
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 168a909fa78b..b47c840b6b54 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -512,14 +512,12 @@ static SDL_AudioDevice *CreatePhysicalAudioDevice(const char *name, SDL_bool isc
 
     SDL_AudioDevice *device = (SDL_AudioDevice *)SDL_calloc(1, sizeof(SDL_AudioDevice));
     if (!device) {
-        SDL_OutOfMemory();
         return NULL;
     }
 
     device->name = SDL_strdup(name);
     if (!device->name) {
         SDL_free(device);
-        SDL_OutOfMemory();
         return NULL;
     }
 
@@ -840,7 +838,7 @@ int SDL_InitAudio(const char *driver_name)
         if (!driver_name_copy) {
             SDL_DestroyRWLock(device_hash_lock);
             SDL_DestroyHashTable(device_hash);
-            return SDL_OutOfMemory();
+            return -1;
         }
 
         while (driver_attempt && *driver_attempt != 0 && !initialized) {
@@ -1272,7 +1270,6 @@ static SDL_AudioDeviceID *GetAudioDevices(int *reqcount, SDL_bool iscapture)
         retval = (SDL_AudioDeviceID *) SDL_malloc((num_devices + 1) * sizeof (SDL_AudioDeviceID));
         if (!retval) {
             num_devices = 0;
-            SDL_OutOfMemory();
         } else {
             int devs_seen = 0;
             const void *key;
@@ -1360,9 +1357,6 @@ char *SDL_GetAudioDeviceName(SDL_AudioDeviceID devid)
     SDL_AudioDevice *device = ObtainPhysicalAudioDevice(devid);
     if (device) {
         retval = SDL_strdup(device->name);
-        if (!retval) {
-            SDL_OutOfMemory();
-        }
     }
     ReleaseAudioDevice(device);
 
@@ -1574,14 +1568,14 @@ static int OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec
     device->work_buffer = (Uint8 *)SDL_aligned_alloc(SDL_SIMDGetAlignment(), device->work_buffer_size);
     if (!device->work_buffer) {
         ClosePhysicalAudioDevice(device);
-        return SDL_OutOfMemory();
+        return -1;
     }
 
     if (device->spec.format != SDL_AUDIO_F32) {
         device->mix_buffer = (Uint8 *)SDL_aligned_alloc(SDL_SIMDGetAlignment(), device->work_buffer_size);
         if (!device->mix_buffer) {
             ClosePhysicalAudioDevice(device);
-            return SDL_OutOfMemory();
+            return -1;
         }
     }
 
@@ -1630,7 +1624,7 @@ SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSp
             // uhoh, this device is undead, and just waiting to be cleaned up. Refuse explicit opens.
             SDL_SetError("Device was already lost and can't accept new opens");
         } else if ((logdev = (SDL_LogicalAudioDevice *) SDL_calloc(1, sizeof (SDL_LogicalAudioDevice))) == NULL) {
-            SDL_OutOfMemory();
+            /* SDL_calloc already called SDL_OutOfMemory */
         } else if (OpenPhysicalAudioDevice(device, spec) == -1) {  // if this is the first thing using this physical device, open at the OS level if necessary...
             SDL_free(logdev);
         } else {
@@ -1704,7 +1698,7 @@ int SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallbac
         if (callback && !device->postmix_buffer) {
             device->postmix_buffer = (float *)SDL_aligned_alloc(SDL_SIMDGetAlignment(), device->work_buffer_size);
             if (!device->postmix_buffer) {
-                retval = SDL_OutOfMemory();
+                retval = -1;
             }
         }
 
diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c
index a10141009570..27f69f7020c7 100644
--- a/src/audio/SDL_audiocvt.c
+++ b/src/audio/SDL_audiocvt.c
@@ -389,7 +389,7 @@ static int UpdateAudioStreamInputSpec(SDL_AudioStream *stream, const SDL_AudioSp
     if (stream->history_buffer_allocation < history_buffer_allocation) {
         history_buffer = (Uint8 *) SDL_aligned_alloc(SDL_SIMDGetAlignment(), history_buffer_allocation);
         if (!history_buffer) {
-            return SDL_OutOfMemory();
+            return -1;
         }
         SDL_aligned_free(stream->history_buffer);
         stream->history_buffer = history_buffer;
@@ -409,7 +409,6 @@ SDL_AudioStream *SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_
 
     SDL_AudioStream *retval = (SDL_AudioStream *)SDL_calloc(1, sizeof(SDL_AudioStream));
     if (!retval) {
-        SDL_OutOfMemory();
         return NULL;
     }
 
@@ -722,7 +721,6 @@ static Uint8 *EnsureAudioStreamWorkBufferSize(SDL_AudioStream *stream, size_t ne
 
     Uint8 *ptr = (Uint8 *) SDL_aligned_alloc(SDL_SIMDGetAlignment(), newlen);
     if (!ptr) {
-        SDL_OutOfMemory();
         return NULL;  // previous work buffer is still valid!
     }
 
@@ -1234,9 +1232,7 @@ int SDL_ConvertAudioSamples(const SDL_AudioSpec *src_spec, const Uint8 *src_data
             dstlen = SDL_GetAudioStreamAvailable(stream);
             if (dstlen >= 0) {
                 dst = (Uint8 *)SDL_malloc(dstlen);
-                if (!dst) {
-                    SDL_OutOfMemory();
-                } else {
+                if (dst) {
                     retval = (SDL_GetAudioStreamData(stream, dst, dstlen) >= 0) ? 0 : -1;
                 }
             }
diff --git a/src/audio/SDL_audioqueue.c b/src/audio/SDL_audioqueue.c
index 763809fdca89..085e8aaee02d 100644
--- a/src/audio/SDL_audioqueue.c
+++ b/src/audio/SDL_audioqueue.c
@@ -150,7 +150,7 @@ static int WriteToChunkedAudioTrack(void *ctx, const Uint8 *data, size_t len)
         chunk = CreateAudioTrackChunk(track);
 
         if (!chunk) {
-            return SDL_OutOfMemory();
+            return -1;
         }
 
         SDL_assert((track->head == NULL) && (track->tail == NULL) && (track->queued_bytes == 0));
@@ -189,7 +189,7 @@ static int WriteToChunkedAudioTrack(void *ctx, const Uint8 *data, size_t len)
 
         DestroyAudioChunks(next);
 
-        return SDL_OutOfMemory();
+        return -1;
     }
 
     track->tail = chunk;
@@ -256,7 +256,6 @@ static SDL_AudioTrack *CreateChunkedAudioTrack(const SDL_AudioSpec *spec, size_t
     SDL_ChunkedAudioTrack *track = (SDL_ChunkedAudioTrack *)SDL_calloc(1, sizeof(*track));
 
     if (!track) {
-        SDL_OutOfMemory();
         return NULL;
     }
 
@@ -276,7 +275,6 @@ SDL_AudioQueue *SDL_CreateAudioQueue(size_t chunk_size)
     SDL_AudioQueue *queue = (SDL_AudioQueue *)SDL_calloc(1, sizeof(*queue));
 
     if (!queue) {
-        SDL_OutOfMemory();
         return NULL;
     }
 
@@ -398,7 +396,7 @@ int SDL_WriteToAudioQueue(SDL_AudioQueue *queue, const SDL_AudioSpec *spec, cons
         SDL_AudioTrack *new_track = CreateChunkedAudioTrack(spec, queue->chunk_size);
 
         if (!new_track) {
-            return SDL_OutOfMemory();
+            return -1;
         }
 
         if (track) {
diff --git a/src/audio/SDL_wave.c b/src/audio/SDL_wave.c
index 76785cf91447..cb3732305d57 100644
--- a/src/audio/SDL_wave.c
+++ b/src/audio/SDL_wave.c
@@ -440,7 +440,7 @@ static int MS_ADPCM_Init(WaveFile *file, size_t datalength)
     coeffdata = (MS_ADPCM_CoeffData *)SDL_malloc(sizeof(MS_ADPCM_CoeffData) + coeffcount * 4);
     file->decoderdata = coeffdata; /* Freed in cleanup. */
     if (!coeffdata) {
-        return SDL_OutOfMemory();
+        return -1;
     }
     coeffdata->coeff = &coeffdata->aligndummy;
     coeffdata->coeffcount = (Uint16)coeffcount;
@@ -674,7 +674,7 @@ static int MS_ADPCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
     /* The output size in bytes. May get modified if data is truncated. */
     outputsize = (size_t)state.framestotal;
     if (SafeMult(&outputsize, state.framesize)) {
-        return SDL_OutOfMemory();
+        return SDL_SetError("WAVE file too big");
     } else if (outputsize > SDL_MAX_UINT32 || state.framestotal > SIZE_MAX) {
         return SDL_SetError("WAVE file too big");
     }
@@ -683,7 +683,7 @@ static int MS_ADPCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
     state.output.size = outputsize / sizeof(Sint16);
     state.output.data = (Sint16 *)SDL_calloc(1, outputsize);
     if (!state.output.data) {
-        return SDL_OutOfMemory();
+        return -1;
     }
 
     state.cstate = cstate;
@@ -1065,7 +1065,7 @@ static int IMA_ADPCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len
     /* The output size in bytes. May get modified if data is truncated. */
     outputsize = (size_t)state.framestotal;
     if (SafeMult(&outputsize, state.framesize)) {
-        return SDL_OutOfMemory();
+        return SDL_SetError("WAVE file too big");
     } else if (outputsize > SDL_MAX_UINT32 || state.framestotal > SIZE_MAX) {
         return SDL_SetError("WAVE file too big");
     }
@@ -1074,13 +1074,13 @@ static int IMA_ADPCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len
     state.output.size = outputsize / sizeof(Sint16);
     state.output.data = (Sint16 *)SDL_malloc(outputsize);
     if (!state.output.data) {
-        return SDL_OutOfMemory();
+        return -1;
     }
 
     cstate = (Sint8 *)SDL_calloc(state.channels, sizeof(Sint8));
     if (!cstate) {
         SDL_free(state.output.data);
-        return SDL_OutOfMemory();
+        return -1;
     }
     state.cstate = cstate;
 
@@ -1221,12 +1221,12 @@ static int LAW_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
 
     sample_count = (size_t)file->sampleframes;
     if (SafeMult(&sample_count, format->channels)) {
-        return SDL_OutOfMemory();
+        return SDL_SetError("WAVE file too big");
     }
 
     expanded_len = sample_count;
     if (SafeMult(&expanded_len, sizeof(Sint16))) {
-        return SDL_OutOfMemory();
+        return SDL_SetError("WAVE file too big");
     } else if (expanded_len > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
         return SDL_SetError("WAVE file too big");
     }
@@ -1234,7 +1234,7 @@ static int LAW_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
     /* 1 to avoid allocating zero bytes, to keep static analysis happy. */
     src = (Uint8 *)SDL_realloc(chunk->data, expanded_len ? expanded_len : 1);
     if (!src) {
-        return SDL_OutOfMemory();
+        return -1;
     }
     chunk->data = NULL;
     chunk->size = 0;
@@ -1352,12 +1352,12 @@ static int PCM_ConvertSint24ToSint32(WaveFile *file, Uint8 **audio_buf, Uint32 *
 
     sample_count = (size_t)file->sampleframes;
     if (SafeMult(&sample_count, format->channels)) {
-        return SDL_OutOfMemory();
+        return SDL_SetError("WAVE file too big");
     }
 
     expanded_len = sample_count;
     if (SafeMult(&expanded_len, sizeof(Sint32))) {
-        return SDL_OutOfMemory();
+        return SDL_SetError("WAVE file too big");
     } else if (expanded_len > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
         return SDL_SetError("WAVE file too big");
     }
@@ -1365,7 +1365,7 @@ static int PCM_ConvertSint24ToSint32(WaveFile *file, Uint8 **audio_buf, Uint32 *
     /* 1 to avoid allocating zero bytes, to keep static analysis happy. */
     ptr = (Uint8 *)SDL_realloc(chunk->data, expanded_len ? expanded_len : 1);
     if (!ptr) {
-        return SDL_OutOfMemory();
+        return -1;
     }
 
     /* This pointer is now invalid. */
@@ -1421,7 +1421,7 @@ static int PCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
 
     outputsize = (size_t)file->sampleframes;
     if (SafeMult(&outputsize, format->blockalign)) {
-        return SDL_OutOfMemory();
+        return SDL_SetError("WAVE file too big");
     } else if (outputsize > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
         return SDL_SetError("WAVE file too big");
     }
@@ -1545,7 +1545,7 @@ static int WaveReadPartialChunkData(SDL_RWops *src, WaveChunk *chunk, size_t len
     if (length > 0) {
         chunk->data = (Uint8 *)SDL_malloc(length);
         if (!chunk->data) {
-            return SDL_OutOfMemory();
+            return -1;
         }
 
         if (SDL_RWseek(src, chunk->position, SDL_RW_SEEK_SET) != chunk->position) {
@@ -1611,7 +1611,7 @@ static int WaveReadFormat(WaveFile *file)
     }
     fmtsrc = SDL_RWFromConstMem(chunk->data, (int)chunk->size);
     if (!fmtsrc) {
-        return SDL_OutOfMemory();
+        return -1;
     }
 
     if (!SDL_ReadU16LE(fmtsrc, &format->formattag) ||
diff --git a/src/audio/aaudio/SDL_aaudio.c b/src/audio/aaudio/SDL_aaudio.c
index 668ffac05ec3..d46f671b67fd 100644
--- a/src/audio/aaudio/SDL_aaudio.c
+++ b/src/audio/aaudio/SDL_aaudio.c
@@ -355,7 +355,7 @@ static int BuildAAudioStream(SDL_AudioDevice *device)
     hidden->mixbuf_bytes = (hidden->num_buffers * device->buffer_size);
     hidden->mixbuf = (Uint8 *)SDL_aligned_alloc(SDL_SIMDGetAlignment(), hidden->mixbuf_bytes);
     if (!hidden->mixbuf) {
-        return SDL_OutOfMemory();
+        return -1;
     }
     hidden->processed_bytes = 0;
     hidden->callback_bytes = 0;
@@ -398,7 +398,7 @@ static int AAUDIO_OpenDevice(SDL_AudioDevice *device)
 
     device->hidden = (struct SDL_PrivateAudioData *)SDL_calloc(1, sizeof(*device->hidden));
     if (!device->hidden) {
-        return SDL_OutOfMemory();
+        return -1;
     }
 
     return BuildAAudioStream(device);
diff --git a/src/audio/alsa/SDL_alsa_audio.c b/src/audio/alsa/SDL_alsa_audio.c
index e7505b88d953..98953a96ae3b 100644
--- a/src/audio/alsa/SDL_alsa_audio.c
+++ b/src/audio/alsa/SDL_alsa_audio.c
@@ -537,7 +537,7 @@ static int ALSA_OpenDevice(SDL_AudioDevice *device)
     // Initialize all variables that we clean on shutdown
     device->hidden = (struct SDL_PrivateAudioData *)SDL_calloc(1, sizeof(*device->hidden));
     if (!device->hidden) {
-        return SDL_OutOfMemory();
+        return -1;
     }
 
     // Open the audio device
@@ -683,7 +683,7 @@ static int ALSA_OpenDevice(SDL_AudioDevice *device)
     if (!iscapture) {
         device->hidden->mixbuf = (Uint8 *)SDL_malloc(device->buffer_size);
         if (!device->hidden->mixbuf) {
-            return SDL_OutOfMemory();
+            return -1;
         }
         SDL_memset(device->hidden->mixbuf, device->silence_value, device->buffer_size);
     }
diff --git a/src/audio/android/SDL_androidaudio.c b/src/audio/android/SDL_androidaudio.c
index b2147f336828..55be7dddd8d0 100644

(Patch may be truncated, please check the link at the top of this post.)