Ensure that all functions that follow the SDL_GetStringRule return temporary memory

This commit is contained in:
Sam Lantinga 2024-07-19 09:50:54 -07:00
parent fd9fe1bb7b
commit 68322ac851
12 changed files with 15 additions and 36 deletions

View file

@ -723,8 +723,6 @@ typedef struct SDL_PixelFormatDetails
/** /**
* Get the human readable name of a pixel format. * Get the human readable name of a pixel format.
* *
* The returned string follows the SDL_GetStringRule, and will be automatically freed later.
*
* \param format the pixel format to query. * \param format the pixel format to query.
* \returns the human readable name of the specified pixel format or * \returns the human readable name of the specified pixel format or
* "SDL_PIXELFORMAT_UNKNOWN" if the format isn't recognized. * "SDL_PIXELFORMAT_UNKNOWN" if the format isn't recognized.

View file

@ -48,8 +48,6 @@ extern "C" {
* - "iOS" * - "iOS"
* - "Android" * - "Android"
* *
* The returned string follows the SDL_GetStringRule, and will be automatically freed later.
*
* \returns the name of the platform. If the correct platform name is not * \returns the name of the platform. If the correct platform name is not
* available, returns a string beginning with the text "Unknown". * available, returns a string beginning with the text "Unknown".
* *

View file

@ -163,8 +163,6 @@ extern SDL_DECLSPEC int SDLCALL SDL_GetVersion(void);
* You shouldn't use this function for anything but logging it for debugging * You shouldn't use this function for anything but logging it for debugging
* purposes. The string is not intended to be reliable in any way. * purposes. The string is not intended to be reliable in any way.
* *
* The returned string follows the SDL_GetStringRule, and will be automatically freed later.
*
* \returns an arbitrary string, uniquely identifying the exact revision of * \returns an arbitrary string, uniquely identifying the exact revision of
* the SDL library in use. * the SDL library in use.
* *

View file

@ -601,11 +601,10 @@ int SDL_GetVersion(void)
/* Get the library source revision */ /* Get the library source revision */
const char *SDL_GetRevision(void) const char *SDL_GetRevision(void)
{ {
return SDL_REVISION; // a string literal, no need to SDL_FreeLater it. return SDL_REVISION;
} }
// Get the name of the platform // Get the name of the platform
// (a string literal, no need to SDL_FreeLater it.)
const char *SDL_GetPlatform(void) const char *SDL_GetPlatform(void)
{ {
#if defined(SDL_PLATFORM_AIX) #if defined(SDL_PLATFORM_AIX)

View file

@ -131,19 +131,17 @@ int SDL_GetNumAudioDrivers(void)
return num_drivers; return num_drivers;
} }
// this returns string literals, so there's no need to use SDL_FreeLater.
const char *SDL_GetAudioDriver(int index) const char *SDL_GetAudioDriver(int index)
{ {
if (index >= 0 && index < SDL_GetNumAudioDrivers()) { if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
return deduped_bootstrap[index]->name; return SDL_CreateTemporaryString(deduped_bootstrap[index]->name);
} }
return NULL; return NULL;
} }
// this returns string literals, so there's no need to use SDL_FreeLater.
const char *SDL_GetCurrentAudioDriver(void) const char *SDL_GetCurrentAudioDriver(void)
{ {
return current_audio.name; return SDL_CreateTemporaryString(current_audio.name);
} }
static int GetDefaultSampleFramesFromFreq(const int freq) static int GetDefaultSampleFramesFromFreq(const int freq)

View file

@ -63,19 +63,17 @@ int SDL_GetNumCameraDrivers(void)
return SDL_arraysize(bootstrap) - 1; return SDL_arraysize(bootstrap) - 1;
} }
// this returns string literals, so there's no need to use SDL_FreeLater.
const char *SDL_GetCameraDriver(int index) const char *SDL_GetCameraDriver(int index)
{ {
if (index >= 0 && index < SDL_GetNumCameraDrivers()) { if (index >= 0 && index < SDL_GetNumCameraDrivers()) {
return bootstrap[index]->name; return SDL_CreateTemporaryString(bootstrap[index]->name);
} }
return NULL; return NULL;
} }
// this returns string literals, so there's no need to use SDL_FreeLater.
const char *SDL_GetCurrentCameraDriver(void) const char *SDL_GetCurrentCameraDriver(void)
{ {
return camera_driver.name; return SDL_CreateTemporaryString(camera_driver.name);
} }
char *SDL_GetCameraThreadName(SDL_Camera *device, char *buf, size_t buflen) char *SDL_GetCameraThreadName(SDL_Camera *device, char *buf, size_t buflen)

View file

@ -2452,7 +2452,6 @@ void SDL_SendAndroidBackButton(void)
(*env)->CallStaticVoidMethod(env, mActivityClass, midManualBackButton); (*env)->CallStaticVoidMethod(env, mActivityClass, midManualBackButton);
} }
// this caches a string until the process ends, so there's no need to use SDL_FreeLater.
const char *SDL_GetAndroidInternalStoragePath(void) const char *SDL_GetAndroidInternalStoragePath(void)
{ {
static char *s_AndroidInternalFilesPath = NULL; static char *s_AndroidInternalFilesPath = NULL;
@ -2504,7 +2503,7 @@ const char *SDL_GetAndroidInternalStoragePath(void)
LocalReferenceHolder_Cleanup(&refs); LocalReferenceHolder_Cleanup(&refs);
} }
return s_AndroidInternalFilesPath; return SDL_CreateTemporaryString(s_AndroidInternalFilesPath);
} }
Uint32 SDL_GetAndroidExternalStorageState(void) Uint32 SDL_GetAndroidExternalStorageState(void)
@ -2547,7 +2546,6 @@ Uint32 SDL_GetAndroidExternalStorageState(void)
return stateFlags; return stateFlags;
} }
// this caches a string until the process ends, so there's no need to use SDL_FreeLater.
const char *SDL_GetAndroidExternalStoragePath(void) const char *SDL_GetAndroidExternalStoragePath(void)
{ {
static char *s_AndroidExternalFilesPath = NULL; static char *s_AndroidExternalFilesPath = NULL;
@ -2590,10 +2588,9 @@ const char *SDL_GetAndroidExternalStoragePath(void)
LocalReferenceHolder_Cleanup(&refs); LocalReferenceHolder_Cleanup(&refs);
} }
return s_AndroidExternalFilesPath; return SDL_CreateTemporaryString(s_AndroidExternalFilesPath);
} }
// this caches a string until the process ends, so there's no need to use SDL_FreeLater.
const char *SDL_GetAndroidCachePath(void) const char *SDL_GetAndroidCachePath(void)
{ {
// !!! FIXME: lots of duplication with SDL_GetAndroidExternalStoragePath and SDL_GetAndroidInternalStoragePath; consolidate these functions! // !!! FIXME: lots of duplication with SDL_GetAndroidExternalStoragePath and SDL_GetAndroidInternalStoragePath; consolidate these functions!
@ -2637,7 +2634,7 @@ const char *SDL_GetAndroidCachePath(void)
LocalReferenceHolder_Cleanup(&refs); LocalReferenceHolder_Cleanup(&refs);
} }
return s_AndroidCachePath; return SDL_CreateTemporaryString(s_AndroidCachePath);
} }
int SDL_ShowAndroidToast(const char *message, int duration, int gravity, int xOffset, int yOffset) int SDL_ShowAndroidToast(const char *message, int duration, int gravity, int xOffset, int yOffset)

View file

@ -945,7 +945,6 @@ int SDL_SetScancodeName(SDL_Scancode scancode, const char *name)
return 0; return 0;
} }
// these are static memory, so we don't use SDL_FreeLater on them.
const char *SDL_GetScancodeName(SDL_Scancode scancode) const char *SDL_GetScancodeName(SDL_Scancode scancode)
{ {
const char *name; const char *name;
@ -955,11 +954,10 @@ const char *SDL_GetScancodeName(SDL_Scancode scancode)
} }
name = SDL_scancode_names[scancode]; name = SDL_scancode_names[scancode];
if (name) { if (!name) {
return name; name = "";
} }
return SDL_CreateTemporaryString(name);
return "";
} }
SDL_Scancode SDL_GetScancodeFromName(const char *name) SDL_Scancode SDL_GetScancodeFromName(const char *name)

View file

@ -97,7 +97,6 @@ static const wchar_t *SDL_GetWinRTFSPathUNICODE(SDL_WinRT_Path pathType)
return NULL; return NULL;
} }
// this caches a string until the process ends, so there's no need to use SDL_FreeLater.
extern "C" const char *SDL_GetWinRTFSPath(SDL_WinRT_Path pathType) extern "C" const char *SDL_GetWinRTFSPath(SDL_WinRT_Path pathType)
{ {
typedef unordered_map<SDL_WinRT_Path, string> UTF8PathMap; typedef unordered_map<SDL_WinRT_Path, string> UTF8PathMap;
@ -116,7 +115,7 @@ extern "C" const char *SDL_GetWinRTFSPath(SDL_WinRT_Path pathType)
char *utf8Path = WIN_StringToUTF8W(ucs2Path); char *utf8Path = WIN_StringToUTF8W(ucs2Path);
utf8Paths[pathType] = utf8Path; utf8Paths[pathType] = utf8Path;
SDL_free(utf8Path); SDL_free(utf8Path);
return utf8Paths[pathType].c_str(); return SDL_CreateTemporaryString(utf8Paths[pathType].c_str());
} }
extern "C" char *SDL_SYS_GetBasePath(void) extern "C" char *SDL_SYS_GetBasePath(void)

View file

@ -799,7 +799,6 @@ int SDL_GetNumRenderDrivers(void)
#endif #endif
} }
// this returns string literals, so there's no need to use SDL_FreeLater.
const char *SDL_GetRenderDriver(int index) const char *SDL_GetRenderDriver(int index)
{ {
#ifndef SDL_RENDER_DISABLED #ifndef SDL_RENDER_DISABLED
@ -808,7 +807,7 @@ const char *SDL_GetRenderDriver(int index)
SDL_GetNumRenderDrivers() - 1); SDL_GetNumRenderDrivers() - 1);
return NULL; return NULL;
} }
return render_drivers[index]->name; return SDL_CreateTemporaryString(render_drivers[index]->name);
#else #else
SDL_SetError("SDL not built with rendering support"); SDL_SetError("SDL not built with rendering support");
return NULL; return NULL;

View file

@ -88,7 +88,6 @@ SDL_COMPILE_TIME_ASSERT(SDL_expand_byte_10_size, SDL_arraysize(SDL_expand_byte_1
/* Helper functions */ /* Helper functions */
// This doesn't need SDL_FreeLater since it returns string literals.
#define CASE(X) \ #define CASE(X) \
case X: \ case X: \
return #X; return #X;

View file

@ -515,11 +515,10 @@ int SDL_GetNumVideoDrivers(void)
return SDL_arraysize(bootstrap) - 1; return SDL_arraysize(bootstrap) - 1;
} }
// this returns string literals, so there's no need to use SDL_FreeLater.
const char *SDL_GetVideoDriver(int index) const char *SDL_GetVideoDriver(int index)
{ {
if (index >= 0 && index < SDL_GetNumVideoDrivers()) { if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
return bootstrap[index]->name; return SDL_CreateTemporaryString(bootstrap[index]->name);
} }
return NULL; return NULL;
} }
@ -657,14 +656,13 @@ pre_driver_error:
return -1; return -1;
} }
// this returns string literals, so there's no need to use SDL_FreeLater.
const char *SDL_GetCurrentVideoDriver(void) const char *SDL_GetCurrentVideoDriver(void)
{ {
if (!_this) { if (!_this) {
SDL_UninitializedVideo(); SDL_UninitializedVideo();
return NULL; return NULL;
} }
return _this->name; return SDL_CreateTemporaryString(_this->name);
} }
SDL_VideoDevice *SDL_GetVideoDevice(void) SDL_VideoDevice *SDL_GetVideoDevice(void)