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.
*
* The returned string follows the SDL_GetStringRule, and will be automatically freed later.
*
* \param format the pixel format to query.
* \returns the human readable name of the specified pixel format or
* "SDL_PIXELFORMAT_UNKNOWN" if the format isn't recognized.

View File

@ -48,8 +48,6 @@ extern "C" {
* - "iOS"
* - "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
* 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
* 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
* the SDL library in use.
*

View File

@ -601,11 +601,10 @@ int SDL_GetVersion(void)
/* Get the library source revision */
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
// (a string literal, no need to SDL_FreeLater it.)
const char *SDL_GetPlatform(void)
{
#if defined(SDL_PLATFORM_AIX)

View File

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

View File

@ -63,19 +63,17 @@ int SDL_GetNumCameraDrivers(void)
return SDL_arraysize(bootstrap) - 1;
}
// this returns string literals, so there's no need to use SDL_FreeLater.
const char *SDL_GetCameraDriver(int index)
{
if (index >= 0 && index < SDL_GetNumCameraDrivers()) {
return bootstrap[index]->name;
return SDL_CreateTemporaryString(bootstrap[index]->name);
}
return NULL;
}
// this returns string literals, so there's no need to use SDL_FreeLater.
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)

View File

@ -2452,7 +2452,6 @@ void SDL_SendAndroidBackButton(void)
(*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)
{
static char *s_AndroidInternalFilesPath = NULL;
@ -2504,7 +2503,7 @@ const char *SDL_GetAndroidInternalStoragePath(void)
LocalReferenceHolder_Cleanup(&refs);
}
return s_AndroidInternalFilesPath;
return SDL_CreateTemporaryString(s_AndroidInternalFilesPath);
}
Uint32 SDL_GetAndroidExternalStorageState(void)
@ -2547,7 +2546,6 @@ Uint32 SDL_GetAndroidExternalStorageState(void)
return stateFlags;
}
// this caches a string until the process ends, so there's no need to use SDL_FreeLater.
const char *SDL_GetAndroidExternalStoragePath(void)
{
static char *s_AndroidExternalFilesPath = NULL;
@ -2590,10 +2588,9 @@ const char *SDL_GetAndroidExternalStoragePath(void)
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)
{
// !!! 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);
}
return s_AndroidCachePath;
return SDL_CreateTemporaryString(s_AndroidCachePath);
}
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;
}
// these are static memory, so we don't use SDL_FreeLater on them.
const char *SDL_GetScancodeName(SDL_Scancode scancode)
{
const char *name;
@ -955,11 +954,10 @@ const char *SDL_GetScancodeName(SDL_Scancode scancode)
}
name = SDL_scancode_names[scancode];
if (name) {
return name;
if (!name) {
name = "";
}
return "";
return SDL_CreateTemporaryString(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;
}
// 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)
{
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);
utf8Paths[pathType] = utf8Path;
SDL_free(utf8Path);
return utf8Paths[pathType].c_str();
return SDL_CreateTemporaryString(utf8Paths[pathType].c_str());
}
extern "C" char *SDL_SYS_GetBasePath(void)

View File

@ -799,7 +799,6 @@ int SDL_GetNumRenderDrivers(void)
#endif
}
// this returns string literals, so there's no need to use SDL_FreeLater.
const char *SDL_GetRenderDriver(int index)
{
#ifndef SDL_RENDER_DISABLED
@ -808,7 +807,7 @@ const char *SDL_GetRenderDriver(int index)
SDL_GetNumRenderDrivers() - 1);
return NULL;
}
return render_drivers[index]->name;
return SDL_CreateTemporaryString(render_drivers[index]->name);
#else
SDL_SetError("SDL not built with rendering support");
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 */
// This doesn't need SDL_FreeLater since it returns string literals.
#define CASE(X) \
case X: \
return #X;

View File

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