Browse Source

Ensure that all functions that follow the SDL_GetStringRule return temporary memory

Sam Lantinga 9 months ago
parent
commit
68322ac851

+ 0 - 2
include/SDL3/SDL_pixels.h

@@ -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.

+ 0 - 2
include/SDL3/SDL_platform.h

@@ -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".
  *

+ 0 - 2
include/SDL3/SDL_version.h

@@ -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.
  *

+ 1 - 2
src/SDL.c

@@ -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)

+ 2 - 4
src/audio/SDL_audio.c

@@ -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)

+ 2 - 4
src/camera/SDL_camera.c

@@ -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)

+ 3 - 6
src/core/android/SDL_android.c

@@ -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)

+ 3 - 5
src/events/SDL_keymap.c

@@ -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)

+ 1 - 2
src/filesystem/winrt/SDL_sysfilesystem.cpp

@@ -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)

+ 1 - 2
src/render/SDL_render.c

@@ -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;

+ 0 - 1
src/video/SDL_pixels.c

@@ -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;

+ 2 - 4
src/video/SDL_video.c

@@ -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)