Fix property cleanup callback not being called on error (#9663)

The documentation for `SDL_SetPropertyWithCleanup` mentions that the cleanup function
is called upon failure. But this wasn't working in the code.
This commit is contained in:
Susko3 2024-05-06 23:50:28 +02:00 committed by GitHub
parent 01d560df50
commit 56feecc17d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 9 deletions

View file

@ -305,11 +305,11 @@ static int SDL_PrivateSetProperty(SDL_PropertiesID props, const char *name, SDL_
int result = 0;
if (!props) {
SDL_FreePropertyWithCleanup(NULL, property, NULL, SDL_FALSE);
SDL_FreePropertyWithCleanup(NULL, property, NULL, SDL_TRUE);
return SDL_InvalidParamError("props");
}
if (!name || !*name) {
SDL_FreePropertyWithCleanup(NULL, property, NULL, SDL_FALSE);
SDL_FreePropertyWithCleanup(NULL, property, NULL, SDL_TRUE);
return SDL_InvalidParamError("name");
}
@ -318,7 +318,7 @@ static int SDL_PrivateSetProperty(SDL_PropertiesID props, const char *name, SDL_
SDL_UnlockMutex(SDL_properties_lock);
if (!properties) {
SDL_FreePropertyWithCleanup(NULL, property, NULL, SDL_FALSE);
SDL_FreePropertyWithCleanup(NULL, property, NULL, SDL_TRUE);
return SDL_InvalidParamError("props");
}
@ -328,7 +328,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_FreePropertyWithCleanup(key, property, NULL, SDL_FALSE);
SDL_FreePropertyWithCleanup(key, property, NULL, SDL_TRUE);
result = -1;
}
}
@ -343,11 +343,17 @@ int SDL_SetPropertyWithCleanup(SDL_PropertiesID props, const char *name, void *v
SDL_Property *property;
if (!value) {
if (cleanup) {
cleanup(userdata, value);
}
return SDL_ClearProperty(props, name);
}
property = (SDL_Property *)SDL_calloc(1, sizeof(*property));
if (!property) {
if (cleanup) {
cleanup(userdata, value);
}
SDL_FreePropertyWithCleanup(NULL, property, NULL, SDL_FALSE);
return -1;
}