From c065a9b1289a1de09981c46a4c5e53e3627fd95c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 27 Jun 2023 06:18:45 -0700
Subject: [PATCH] Fix some issues caught by check_stdlib_usage.py
---
.../{stdlib_checks.py => check_stdlib_usage.py} | 3 ++-
src/SDL_assert.c | 2 +-
src/audio/alsa/SDL_alsa_audio.c | 14 +++++++-------
src/audio/n3ds/SDL_n3dsaudio.c | 2 +-
src/audio/ps2/SDL_ps2audio.c | 7 +++----
src/audio/psp/SDL_pspaudio.c | 7 +++----
src/audio/vita/SDL_vitaaudio.c | 7 +++----
src/core/openbsd/SDL_wscons_kbd.c | 8 ++++----
src/core/openbsd/SDL_wscons_mouse.c | 4 ++--
src/cpuinfo/SDL_cpuinfo.c | 2 +-
src/filesystem/ps2/SDL_sysfilesystem.c | 4 ++--
src/filesystem/psp/SDL_sysfilesystem.c | 2 +-
src/render/ps2/SDL_render_ps2.c | 4 ++--
src/video/ngage/SDL_ngagevideo.cpp | 2 +-
src/video/uikit/SDL_uikitappdelegate.m | 12 ++++++++----
15 files changed, 41 insertions(+), 39 deletions(-)
rename build-scripts/{stdlib_checks.py => check_stdlib_usage.py} (98%)
diff --git a/build-scripts/stdlib_checks.py b/build-scripts/check_stdlib_usage.py
similarity index 98%
rename from build-scripts/stdlib_checks.py
rename to build-scripts/check_stdlib_usage.py
index d710c9096797..830a04a3ca0f 100755
--- a/build-scripts/stdlib_checks.py
+++ b/build-scripts/check_stdlib_usage.py
@@ -81,6 +81,7 @@
'lroundf',
'ltoa',
'malloc',
+ 'memalign',
'memcmp',
'memcpy',
'memcpy4',
@@ -199,7 +200,7 @@ def find_symbols_in_file(file, regex):
if regex.match(l):
# free() allowed here
- if "This should NOT be SDL_free" in l:
+ if "This should NOT be SDL_" in l:
continue
# double check
diff --git a/src/SDL_assert.c b/src/SDL_assert.c
index 50ac16572275..acde7f4a0f18 100644
--- a/src/SDL_assert.c
+++ b/src/SDL_assert.c
@@ -270,7 +270,7 @@ static SDL_AssertState SDLCALL SDL_PromptAssertion(const SDL_AssertData *data, v
} else {
okay = SDL_FALSE;
}
- free(buf);
+ free(buf); /* This should NOT be SDL_free() */
if (okay) {
break;
diff --git a/src/audio/alsa/SDL_alsa_audio.c b/src/audio/alsa/SDL_alsa_audio.c
index 27e5557ea4ea..a73204768902 100644
--- a/src/audio/alsa/SDL_alsa_audio.c
+++ b/src/audio/alsa/SDL_alsa_audio.c
@@ -622,7 +622,7 @@ static int ALSA_OpenDevice(SDL_AudioDevice *_this, const char *devname)
_this->hidden->swizzle_func = no_swizzle;
}
}
- free(chmap);
+ free(chmap); /* This should NOT be SDL_free() */
}
#endif /* SND_CHMAP_API_VERSION */
@@ -743,7 +743,7 @@ static void add_device(const int iscapture, const char *name, void *hint, ALSA_D
handle = SDL_strdup(name);
if (handle == NULL) {
if (hint) {
- free(desc);
+ free(desc); /* This should NOT be SDL_free() */
}
SDL_free(dev);
return;
@@ -755,7 +755,7 @@ static void add_device(const int iscapture, const char *name, void *hint, ALSA_D
*/
SDL_AddAudioDevice(iscapture, desc, NULL, handle);
if (hint) {
- free(desc);
+ free(desc); /* This should NOT be SDL_free() */
}
dev->name = handle;
dev->iscapture = iscapture;
@@ -814,7 +814,7 @@ static void ALSA_HotplugIteration(void)
}
}
- free(name);
+ free(name); /* This should NOT be SDL_free() */
}
/* look through the list of device names to find matches */
@@ -839,10 +839,10 @@ static void ALSA_HotplugIteration(void)
SDL_bool have_output = SDL_FALSE;
SDL_bool have_input = SDL_FALSE;
- free(ioid);
+ free(ioid); /* This should NOT be SDL_free() */
if (!isoutput && !isinput) {
- free(name);
+ free(name); /* This should NOT be SDL_free() */
continue;
}
@@ -876,7 +876,7 @@ static void ALSA_HotplugIteration(void)
}
}
- free(name);
+ free(name); /* This should NOT be SDL_free() */
}
ALSA_snd_device_name_free_hint(hints);
diff --git a/src/audio/n3ds/SDL_n3dsaudio.c b/src/audio/n3ds/SDL_n3dsaudio.c
index 254bca0198f3..1d0bff66ef7c 100644
--- a/src/audio/n3ds/SDL_n3dsaudio.c
+++ b/src/audio/n3ds/SDL_n3dsaudio.c
@@ -238,7 +238,7 @@ static void N3DSAUDIO_CloseDevice(SDL_AudioDevice *_this)
if (!_this->hidden->isCancelled) {
ndspChnReset(0);
- memset(_this->hidden->waveBuf, 0, sizeof(ndspWaveBuf) * NUM_BUFFERS);
+ SDL_memset(_this->hidden->waveBuf, 0, sizeof(ndspWaveBuf) * NUM_BUFFERS);
CondVar_Broadcast(&_this->hidden->cv);
}
diff --git a/src/audio/ps2/SDL_ps2audio.c b/src/audio/ps2/SDL_ps2audio.c
index 362f1e0796b6..f14eb6ee7bbc 100644
--- a/src/audio/ps2/SDL_ps2audio.c
+++ b/src/audio/ps2/SDL_ps2audio.c
@@ -26,7 +26,6 @@
#include "SDL_ps2audio.h"
#include <kernel.h>
-#include <malloc.h>
#include <audsrv.h>
#include <ps2_audio_driver.h>
@@ -75,7 +74,7 @@ static int PS2AUDIO_OpenDevice(SDL_AudioDevice *_this, const char *devname)
audsrv_set_volume(MAX_VOLUME);
if (_this->hidden->channel < 0) {
- free(_this->hidden->rawbuf);
+ SDL_aligned_free(_this->hidden->rawbuf);
_this->hidden->rawbuf = NULL;
return SDL_SetError("Couldn't reserve hardware channel");
}
@@ -87,7 +86,7 @@ static int PS2AUDIO_OpenDevice(SDL_AudioDevice *_this, const char *devname)
be a multiple of 64 bytes. Our sample count is already a multiple of
64, so spec->size should be a multiple of 64 as well. */
mixlen = _this->spec.size * NUM_BUFFERS;
- _this->hidden->rawbuf = (Uint8 *)memalign(64, mixlen);
+ _this->hidden->rawbuf = (Uint8 *)SDL_aligned_alloc(64, mixlen);
if (_this->hidden->rawbuf == NULL) {
return SDL_SetError("Couldn't allocate mixing buffer");
}
@@ -128,7 +127,7 @@ static void PS2AUDIO_CloseDevice(SDL_AudioDevice *_this)
}
if (_this->hidden->rawbuf != NULL) {
- free(_this->hidden->rawbuf);
+ SDL_aligned_free(_this->hidden->rawbuf);
_this->hidden->rawbuf = NULL;
}
}
diff --git a/src/audio/psp/SDL_pspaudio.c b/src/audio/psp/SDL_pspaudio.c
index 5d35c373eb52..eda9c74ee33e 100644
--- a/src/audio/psp/SDL_pspaudio.c
+++ b/src/audio/psp/SDL_pspaudio.c
@@ -25,7 +25,6 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
-#include <malloc.h> /* memalign() */
#include "../SDL_audio_c.h"
#include "../SDL_audiodev_c.h"
@@ -92,7 +91,7 @@ static int PSPAUDIO_OpenDevice(SDL_AudioDevice *_this, const char *devname)
}
if (_this->hidden->channel < 0) {
- free(_this->hidden->rawbuf);
+ SDL_aligned_free(_this->hidden->rawbuf);
_this->hidden->rawbuf = NULL;
return SDL_SetError("Couldn't reserve hardware channel");
}
@@ -104,7 +103,7 @@ static int PSPAUDIO_OpenDevice(SDL_AudioDevice *_this, const char *devname)
be a multiple of 64 bytes. Our sample count is already a multiple of
64, so spec->size should be a multiple of 64 as well. */
mixlen = _this->spec.size * NUM_BUFFERS;
- _this->hidden->rawbuf = (Uint8 *)memalign(64, mixlen);
+ _this->hidden->rawbuf = (Uint8 *)SDL_aligned_alloc(64, mixlen);
if (_this->hidden->rawbuf == NULL) {
return SDL_SetError("Couldn't allocate mixing buffer");
}
@@ -154,7 +153,7 @@ static void PSPAUDIO_CloseDevice(SDL_AudioDevice *_this)
}
if (_this->hidden->rawbuf != NULL) {
- free(_this->hidden->rawbuf);
+ SDL_aligned_free(_this->hidden->rawbuf);
_this->hidden->rawbuf = NULL;
}
}
diff --git a/src/audio/vita/SDL_vitaaudio.c b/src/audio/vita/SDL_vitaaudio.c
index 0c76c53432e9..5cd40e69960d 100644
--- a/src/audio/vita/SDL_vitaaudio.c
+++ b/src/audio/vita/SDL_vitaaudio.c
@@ -25,7 +25,6 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
-#include <malloc.h> /* memalign() */
#include "../SDL_audio_c.h"
#include "../SDL_audiodev_c.h"
@@ -96,7 +95,7 @@ static int VITAAUD_OpenDevice(SDL_AudioDevice *_this, const char *devname)
be a multiple of 64 bytes. Our sample count is already a multiple of
64, so spec->size should be a multiple of 64 as well. */
mixlen = _this->spec.size * NUM_BUFFERS;
- _this->hidden->rawbuf = (Uint8 *)memalign(64, mixlen);
+ _this->hidden->rawbuf = (Uint8 *)SDL_aligned_alloc(64, mixlen);
if (_this->hidden->rawbuf == NULL) {
return SDL_SetError("Couldn't allocate mixing buffer");
}
@@ -114,7 +113,7 @@ static int VITAAUD_OpenDevice(SDL_AudioDevice *_this, const char *devname)
_this->hidden->port = sceAudioOutOpenPort(port, _this->spec.samples, _this->spec.freq, format);
if (_this->hidden->port < 0) {
- free(_this->hidden->rawbuf);
+ SDL_aligned_free(_this->hidden->rawbuf);
_this->hidden->rawbuf = NULL;
return SDL_SetError("Couldn't open audio out port: %x", _this->hidden->port);
}
@@ -162,7 +161,7 @@ static void VITAAUD_CloseDevice(SDL_AudioDevice *_this)
}
if (!_this->iscapture && _this->hidden->rawbuf != NULL) {
- free(_this->hidden->rawbuf); /* this uses memalign(), not SDL_malloc(). */
+ SDL_aligned_free(_this->hidden->rawbuf);
_this->hidden->rawbuf = NULL;
}
}
diff --git a/src/core/openbsd/SDL_wscons_kbd.c b/src/core/openbsd/SDL_wscons_kbd.c
index afb6e7cba9cf..f54f1d3951dd 100644
--- a/src/core/openbsd/SDL_wscons_kbd.c
+++ b/src/core/openbsd/SDL_wscons_kbd.c
@@ -42,7 +42,7 @@
#define RETIFIOCTLERR(x) \
if ((x) == -1) { \
- free(input); \
+ SDL_free(input); \
input = NULL; \
return NULL; \
}
@@ -424,13 +424,13 @@ static SDL_WSCONS_input_data *SDL_WSCONS_Init_Keyboard(const char *dev)
}
input->fd = open(dev, O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (input->fd == -1) {
- free(input);
+ SDL_free(input);
input = NULL;
return NULL;
}
input->keymap.map = SDL_calloc(sizeof(struct wscons_keymap), KS_NUMKEYCODES);
if (input->keymap.map == NULL) {
- free(input);
+ SDL_free(input);
return NULL;
}
input->keymap.maplen = KS_NUMKEYCODES;
@@ -471,7 +471,7 @@ void SDL_WSCONS_Quit()
close(input->fd);
input->fd = -1;
}
- free(input);
+ SDL_free(input);
input = NULL;
}
inputs[i] = NULL;
diff --git a/src/core/openbsd/SDL_wscons_mouse.c b/src/core/openbsd/SDL_wscons_mouse.c
index c45dc64637bc..c3378153c8e2 100644
--- a/src/core/openbsd/SDL_wscons_mouse.c
+++ b/src/core/openbsd/SDL_wscons_mouse.c
@@ -46,7 +46,7 @@ SDL_WSCONS_mouse_input_data *SDL_WSCONS_Init_Mouse()
}
mouseInputData->fd = open("/dev/wsmouse", O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (mouseInputData->fd == -1) {
- free(mouseInputData);
+ SDL_free(mouseInputData);
return NULL;
}
#ifdef WSMOUSEIO_SETMODE
@@ -129,5 +129,5 @@ void SDL_WSCONS_Quit_Mouse(SDL_WSCONS_mouse_input_data *inputData)
return;
}
close(inputData->fd);
- free(inputData);
+ SDL_free(inputData);
}
diff --git a/src/cpuinfo/SDL_cpuinfo.c b/src/cpuinfo/SDL_cpuinfo.c
index cb39b0117a94..7eeb14e0cac3 100644
--- a/src/cpuinfo/SDL_cpuinfo.c
+++ b/src/cpuinfo/SDL_cpuinfo.c
@@ -1066,7 +1066,7 @@ int SDL_GetSystemRAM(void)
if (get_system_info(&info) == B_OK) {
/* To have an accurate amount, we also take in account the inaccessible pages (aka ignored)
which is a bit handier compared to the legacy system's api (i.e. used_pages).*/
- SDL_SystemRAM = (int)round((info.max_pages + info.ignored_pages > 0 ? info.ignored_pages : 0) * B_PAGE_SIZE / 1048576.0);
+ SDL_SystemRAM = (int)SDL_round((info.max_pages + info.ignored_pages > 0 ? info.ignored_pages : 0) * B_PAGE_SIZE / 1048576.0);
}
}
#endif
diff --git a/src/filesystem/ps2/SDL_sysfilesystem.c b/src/filesystem/ps2/SDL_sysfilesystem.c
index 92e87a4d07cf..0636fce28585 100644
--- a/src/filesystem/ps2/SDL_sysfilesystem.c
+++ b/src/filesystem/ps2/SDL_sysfilesystem.c
@@ -70,7 +70,7 @@ static void recursive_mkdir(const char *dir)
}
}
- free(base);
+ SDL_free(base);
mkdir(tmp, S_IRWXU);
}
@@ -95,7 +95,7 @@ char *SDL_GetPrefPath(const char *org, const char *app)
} else {
SDL_snprintf(retval, len, "%s%s/", base, app);
}
- free(base);
+ SDL_free(base);
recursive_mkdir(retval);
diff --git a/src/filesystem/psp/SDL_sysfilesystem.c b/src/filesystem/psp/SDL_sysfilesystem.c
index 60c510e5e40e..3fdd7192768f 100644
--- a/src/filesystem/psp/SDL_sysfilesystem.c
+++ b/src/filesystem/psp/SDL_sysfilesystem.c
@@ -63,7 +63,7 @@ char *SDL_GetPrefPath(const char *org, const char *app)
} else {
SDL_snprintf(retval, len, "%s%s/", base, app);
}
- free(base);
+ SDL_free(base);
mkdir(retval, 0755);
return retval;
diff --git a/src/render/ps2/SDL_render_ps2.c b/src/render/ps2/SDL_render_ps2.c
index 9a9708cd4c52..a1828286a6cc 100644
--- a/src/render/ps2/SDL_render_ps2.c
+++ b/src/render/ps2/SDL_render_ps2.c
@@ -111,7 +111,7 @@ static int PS2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
ps2_tex->Width = texture->w;
ps2_tex->Height = texture->h;
ps2_tex->PSM = PixelFormatToPS2PSM(texture->format);
- ps2_tex->Mem = memalign(128, gsKit_texture_size_ee(ps2_tex->Width, ps2_tex->Height, ps2_tex->PSM));
+ ps2_tex->Mem = SDL_aligned_alloc(128, gsKit_texture_size_ee(ps2_tex->Width, ps2_tex->Height, ps2_tex->PSM));
if (!ps2_tex->Mem) {
SDL_free(ps2_tex);
@@ -541,7 +541,7 @@ static void PS2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
// Free from vram
gsKit_TexManager_free(data->gsGlobal, ps2_texture);
- SDL_free(ps2_texture->Mem);
+ SDL_aligned_free(ps2_texture->Mem);
SDL_free(ps2_texture);
texture->driverdata = NULL;
}
diff --git a/src/video/ngage/SDL_ngagevideo.cpp b/src/video/ngage/SDL_ngagevideo.cpp
index 85f54bfc7f95..739b20106b40 100644
--- a/src/video/ngage/SDL_ngagevideo.cpp
+++ b/src/video/ngage/SDL_ngagevideo.cpp
@@ -67,7 +67,7 @@ static void NGAGE_DeleteDevice(SDL_VideoDevice *device)
phdata->NGAGE_WsSession.RedrawReadyCancel();
}
- free(phdata->NGAGE_DrawDevice);
+ free(phdata->NGAGE_DrawDevice); /* This should NOT be SDL_free() */
if (phdata->NGAGE_WsWindow.WsHandle()) {
phdata->NGAGE_WsWindow.Close();
diff --git a/src/video/uikit/SDL_uikitappdelegate.m b/src/video/uikit/SDL_uikitappdelegate.m
index 716941a5b2cf..4b93b8d2c03e 100644
--- a/src/video/uikit/SDL_uikitappdelegate.m
+++ b/src/video/uikit/SDL_uikitappdelegate.m
@@ -52,11 +52,15 @@ int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserv
int i;
/* store arguments */
+ /* Note that we need to be careful about how we allocate/free memory here.
+ * If the application calls SDL_SetMemoryFunctions(), we can't rely on
+ * SDL_free() to use the same allocator after SDL_main() returns.
+ */
forward_main = mainFunction;
forward_argc = argc;
- forward_argv = (char **)malloc((argc + 1) * sizeof(char *));
+ forward_argv = (char **)malloc((argc + 1) * sizeof(char *)); /* This should NOT be SDL_malloc() */
for (i = 0; i < argc; i++) {
- forward_argv[i] = malloc((strlen(argv[i]) + 1) * sizeof(char));
+ forward_argv[i] = malloc((strlen(argv[i]) + 1) * sizeof(char)); /* This should NOT be SDL_malloc() */
strcpy(forward_argv[i], argv[i]);
}
forward_argv[i] = NULL;
@@ -68,9 +72,9 @@ int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserv
/* free the memory we used to hold copies of argc and argv */
for (i = 0; i < forward_argc; i++) {
- free(forward_argv[i]);
+ free(forward_argv[i]); /* This should NOT be SDL_free() */
}
- free(forward_argv);
+ free(forward_argv); /* This should NOT be SDL_free() */
return exit_status;
}