Fix some issues caught by check_stdlib_usage.py

This commit is contained in:
Sam Lantinga 2023-06-27 06:18:45 -07:00
parent e8d2ccbc1c
commit c065a9b128
15 changed files with 41 additions and 39 deletions

View file

@ -81,6 +81,7 @@ words = [
'lroundf', 'lroundf',
'ltoa', 'ltoa',
'malloc', 'malloc',
'memalign',
'memcmp', 'memcmp',
'memcpy', 'memcpy',
'memcpy4', 'memcpy4',
@ -199,7 +200,7 @@ def find_symbols_in_file(file, regex):
if regex.match(l): if regex.match(l):
# free() allowed here # free() allowed here
if "This should NOT be SDL_free" in l: if "This should NOT be SDL_" in l:
continue continue
# double check # double check

View file

@ -270,7 +270,7 @@ static SDL_AssertState SDLCALL SDL_PromptAssertion(const SDL_AssertData *data, v
} else { } else {
okay = SDL_FALSE; okay = SDL_FALSE;
} }
free(buf); free(buf); /* This should NOT be SDL_free() */
if (okay) { if (okay) {
break; break;

View file

@ -622,7 +622,7 @@ static int ALSA_OpenDevice(SDL_AudioDevice *_this, const char *devname)
_this->hidden->swizzle_func = no_swizzle; _this->hidden->swizzle_func = no_swizzle;
} }
} }
free(chmap); free(chmap); /* This should NOT be SDL_free() */
} }
#endif /* SND_CHMAP_API_VERSION */ #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); handle = SDL_strdup(name);
if (handle == NULL) { if (handle == NULL) {
if (hint) { if (hint) {
free(desc); free(desc); /* This should NOT be SDL_free() */
} }
SDL_free(dev); SDL_free(dev);
return; 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); SDL_AddAudioDevice(iscapture, desc, NULL, handle);
if (hint) { if (hint) {
free(desc); free(desc); /* This should NOT be SDL_free() */
} }
dev->name = handle; dev->name = handle;
dev->iscapture = iscapture; 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 */ /* 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_output = SDL_FALSE;
SDL_bool have_input = SDL_FALSE; SDL_bool have_input = SDL_FALSE;
free(ioid); free(ioid); /* This should NOT be SDL_free() */
if (!isoutput && !isinput) { if (!isoutput && !isinput) {
free(name); free(name); /* This should NOT be SDL_free() */
continue; 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); ALSA_snd_device_name_free_hint(hints);

View file

@ -238,7 +238,7 @@ static void N3DSAUDIO_CloseDevice(SDL_AudioDevice *_this)
if (!_this->hidden->isCancelled) { if (!_this->hidden->isCancelled) {
ndspChnReset(0); 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); CondVar_Broadcast(&_this->hidden->cv);
} }

View file

@ -26,7 +26,6 @@
#include "SDL_ps2audio.h" #include "SDL_ps2audio.h"
#include <kernel.h> #include <kernel.h>
#include <malloc.h>
#include <audsrv.h> #include <audsrv.h>
#include <ps2_audio_driver.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); audsrv_set_volume(MAX_VOLUME);
if (_this->hidden->channel < 0) { if (_this->hidden->channel < 0) {
free(_this->hidden->rawbuf); SDL_aligned_free(_this->hidden->rawbuf);
_this->hidden->rawbuf = NULL; _this->hidden->rawbuf = NULL;
return SDL_SetError("Couldn't reserve hardware channel"); 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 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. */ 64, so spec->size should be a multiple of 64 as well. */
mixlen = _this->spec.size * NUM_BUFFERS; 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) { if (_this->hidden->rawbuf == NULL) {
return SDL_SetError("Couldn't allocate mixing buffer"); return SDL_SetError("Couldn't allocate mixing buffer");
} }
@ -128,7 +127,7 @@ static void PS2AUDIO_CloseDevice(SDL_AudioDevice *_this)
} }
if (_this->hidden->rawbuf != NULL) { if (_this->hidden->rawbuf != NULL) {
free(_this->hidden->rawbuf); SDL_aligned_free(_this->hidden->rawbuf);
_this->hidden->rawbuf = NULL; _this->hidden->rawbuf = NULL;
} }
} }

View file

@ -25,7 +25,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <malloc.h> /* memalign() */
#include "../SDL_audio_c.h" #include "../SDL_audio_c.h"
#include "../SDL_audiodev_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) { if (_this->hidden->channel < 0) {
free(_this->hidden->rawbuf); SDL_aligned_free(_this->hidden->rawbuf);
_this->hidden->rawbuf = NULL; _this->hidden->rawbuf = NULL;
return SDL_SetError("Couldn't reserve hardware channel"); 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 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. */ 64, so spec->size should be a multiple of 64 as well. */
mixlen = _this->spec.size * NUM_BUFFERS; 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) { if (_this->hidden->rawbuf == NULL) {
return SDL_SetError("Couldn't allocate mixing buffer"); return SDL_SetError("Couldn't allocate mixing buffer");
} }
@ -154,7 +153,7 @@ static void PSPAUDIO_CloseDevice(SDL_AudioDevice *_this)
} }
if (_this->hidden->rawbuf != NULL) { if (_this->hidden->rawbuf != NULL) {
free(_this->hidden->rawbuf); SDL_aligned_free(_this->hidden->rawbuf);
_this->hidden->rawbuf = NULL; _this->hidden->rawbuf = NULL;
} }
} }

View file

@ -25,7 +25,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <malloc.h> /* memalign() */
#include "../SDL_audio_c.h" #include "../SDL_audio_c.h"
#include "../SDL_audiodev_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 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. */ 64, so spec->size should be a multiple of 64 as well. */
mixlen = _this->spec.size * NUM_BUFFERS; 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) { if (_this->hidden->rawbuf == NULL) {
return SDL_SetError("Couldn't allocate mixing buffer"); 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); _this->hidden->port = sceAudioOutOpenPort(port, _this->spec.samples, _this->spec.freq, format);
if (_this->hidden->port < 0) { if (_this->hidden->port < 0) {
free(_this->hidden->rawbuf); SDL_aligned_free(_this->hidden->rawbuf);
_this->hidden->rawbuf = NULL; _this->hidden->rawbuf = NULL;
return SDL_SetError("Couldn't open audio out port: %x", _this->hidden->port); 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) { 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; _this->hidden->rawbuf = NULL;
} }
} }

View file

@ -42,7 +42,7 @@
#define RETIFIOCTLERR(x) \ #define RETIFIOCTLERR(x) \
if ((x) == -1) { \ if ((x) == -1) { \
free(input); \ SDL_free(input); \
input = NULL; \ input = NULL; \
return 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); input->fd = open(dev, O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (input->fd == -1) { if (input->fd == -1) {
free(input); SDL_free(input);
input = NULL; input = NULL;
return NULL; return NULL;
} }
input->keymap.map = SDL_calloc(sizeof(struct wscons_keymap), KS_NUMKEYCODES); input->keymap.map = SDL_calloc(sizeof(struct wscons_keymap), KS_NUMKEYCODES);
if (input->keymap.map == NULL) { if (input->keymap.map == NULL) {
free(input); SDL_free(input);
return NULL; return NULL;
} }
input->keymap.maplen = KS_NUMKEYCODES; input->keymap.maplen = KS_NUMKEYCODES;
@ -471,7 +471,7 @@ void SDL_WSCONS_Quit()
close(input->fd); close(input->fd);
input->fd = -1; input->fd = -1;
} }
free(input); SDL_free(input);
input = NULL; input = NULL;
} }
inputs[i] = NULL; inputs[i] = NULL;

View file

@ -46,7 +46,7 @@ SDL_WSCONS_mouse_input_data *SDL_WSCONS_Init_Mouse()
} }
mouseInputData->fd = open("/dev/wsmouse", O_RDWR | O_NONBLOCK | O_CLOEXEC); mouseInputData->fd = open("/dev/wsmouse", O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (mouseInputData->fd == -1) { if (mouseInputData->fd == -1) {
free(mouseInputData); SDL_free(mouseInputData);
return NULL; return NULL;
} }
#ifdef WSMOUSEIO_SETMODE #ifdef WSMOUSEIO_SETMODE
@ -129,5 +129,5 @@ void SDL_WSCONS_Quit_Mouse(SDL_WSCONS_mouse_input_data *inputData)
return; return;
} }
close(inputData->fd); close(inputData->fd);
free(inputData); SDL_free(inputData);
} }

View file

@ -1066,7 +1066,7 @@ int SDL_GetSystemRAM(void)
if (get_system_info(&info) == B_OK) { if (get_system_info(&info) == B_OK) {
/* To have an accurate amount, we also take in account the inaccessible pages (aka ignored) /* 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).*/ 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 #endif

View file

@ -70,7 +70,7 @@ static void recursive_mkdir(const char *dir)
} }
} }
free(base); SDL_free(base);
mkdir(tmp, S_IRWXU); mkdir(tmp, S_IRWXU);
} }
@ -95,7 +95,7 @@ char *SDL_GetPrefPath(const char *org, const char *app)
} else { } else {
SDL_snprintf(retval, len, "%s%s/", base, app); SDL_snprintf(retval, len, "%s%s/", base, app);
} }
free(base); SDL_free(base);
recursive_mkdir(retval); recursive_mkdir(retval);

View file

@ -63,7 +63,7 @@ char *SDL_GetPrefPath(const char *org, const char *app)
} else { } else {
SDL_snprintf(retval, len, "%s%s/", base, app); SDL_snprintf(retval, len, "%s%s/", base, app);
} }
free(base); SDL_free(base);
mkdir(retval, 0755); mkdir(retval, 0755);
return retval; return retval;

View file

@ -111,7 +111,7 @@ static int PS2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
ps2_tex->Width = texture->w; ps2_tex->Width = texture->w;
ps2_tex->Height = texture->h; ps2_tex->Height = texture->h;
ps2_tex->PSM = PixelFormatToPS2PSM(texture->format); 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) { if (!ps2_tex->Mem) {
SDL_free(ps2_tex); SDL_free(ps2_tex);
@ -541,7 +541,7 @@ static void PS2_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
// Free from vram // Free from vram
gsKit_TexManager_free(data->gsGlobal, ps2_texture); gsKit_TexManager_free(data->gsGlobal, ps2_texture);
SDL_free(ps2_texture->Mem); SDL_aligned_free(ps2_texture->Mem);
SDL_free(ps2_texture); SDL_free(ps2_texture);
texture->driverdata = NULL; texture->driverdata = NULL;
} }

View file

@ -67,7 +67,7 @@ static void NGAGE_DeleteDevice(SDL_VideoDevice *device)
phdata->NGAGE_WsSession.RedrawReadyCancel(); phdata->NGAGE_WsSession.RedrawReadyCancel();
} }
free(phdata->NGAGE_DrawDevice); free(phdata->NGAGE_DrawDevice); /* This should NOT be SDL_free() */
if (phdata->NGAGE_WsWindow.WsHandle()) { if (phdata->NGAGE_WsWindow.WsHandle()) {
phdata->NGAGE_WsWindow.Close(); phdata->NGAGE_WsWindow.Close();

View file

@ -52,11 +52,15 @@ int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserv
int i; int i;
/* store arguments */ /* 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_main = mainFunction;
forward_argc = argc; 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++) { 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]); strcpy(forward_argv[i], argv[i]);
} }
forward_argv[i] = NULL; 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 */ /* free the memory we used to hold copies of argc and argv */
for (i = 0; i < forward_argc; i++) { 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; return exit_status;
} }