SDL: Don't call the property cleanup function if setting the property fails

From 6cfce101fbdc99dfbf8af33576489e751a752f08 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 4 Dec 2023 07:48:13 -0800
Subject: [PATCH] Don't call the property cleanup function if setting the
 property fails

Fixes https://github.com/libsdl-org/SDL/issues/8659
---
 src/SDL_properties.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/SDL_properties.c b/src/SDL_properties.c
index b883a4d37dcd..27271a487299 100644
--- a/src/SDL_properties.c
+++ b/src/SDL_properties.c
@@ -54,13 +54,13 @@ static SDL_PropertiesID SDL_last_properties_id;
 static SDL_PropertiesID SDL_global_properties;
 
 
-static void SDL_FreeProperty(const void *key, const void *value, void *data)
+static void SDL_FreePropertyWithCleanup(const void *key, const void *value, void *data, SDL_bool cleanup)
 {
     SDL_Property *property = (SDL_Property *)value;
     if (property) {
         switch (property->type) {
         case SDL_PROPERTY_TYPE_POINTER:
-            if (property->cleanup) {
+            if (property->cleanup && cleanup) {
                 property->cleanup(property->userdata, property->value.pointer_value);
             }
             break;
@@ -78,6 +78,11 @@ static void SDL_FreeProperty(const void *key, const void *value, void *data)
     SDL_free((void *)value);
 }
 
+static void SDL_FreeProperty(const void *key, const void *value, void *data)
+{
+    SDL_FreePropertyWithCleanup(key, value, data, SDL_TRUE);
+}
+
 static void SDL_FreeProperties(const void *key, const void *value, void *data)
 {
     SDL_Properties *properties = (SDL_Properties *)value;
@@ -228,11 +233,11 @@ static int SDL_PrivateSetProperty(SDL_PropertiesID props, const char *name, SDL_
     int result = 0;
 
     if (!props) {
-        SDL_FreeProperty(NULL, property, NULL);
+        SDL_FreePropertyWithCleanup(NULL, property, NULL, SDL_FALSE);
         return SDL_InvalidParamError("props");
     }
     if (!name || !*name) {
-        SDL_FreeProperty(NULL, property, NULL);
+        SDL_FreePropertyWithCleanup(NULL, property, NULL, SDL_FALSE);
         return SDL_InvalidParamError("name");
     }
 
@@ -241,7 +246,7 @@ static int SDL_PrivateSetProperty(SDL_PropertiesID props, const char *name, SDL_
     SDL_UnlockMutex(SDL_properties_lock);
 
     if (!properties) {
-        SDL_FreeProperty(NULL, property, NULL);
+        SDL_FreePropertyWithCleanup(NULL, property, NULL, SDL_FALSE);
         return SDL_InvalidParamError("props");
     }
 
@@ -251,7 +256,7 @@ static int SDL_PrivateSetProperty(SDL_PropertiesID props, const char *name, SDL_
         if (property) {
             char *key = SDL_strdup(name);
             if (!SDL_InsertIntoHashTable(properties->props, key, property)) {
-                SDL_FreeProperty(key, property, NULL);
+                SDL_FreePropertyWithCleanup(key, property, NULL, SDL_FALSE);
                 result = -1;
             }
         }