Make sure we always copy the data returned using SDL_GetStringRule

This prevents race conditions where calling an API from one thread returns the data and it's freed by updates on another thread
This commit is contained in:
Sam Lantinga committed 2024-07-19 12:22:03 -07:00
1 parent ef884c8aa6
commit bb96320cc4
19 files changed
+66 -71

No files matched your search

+5 -5
View File
@@ -170,7 +170,7 @@ static int GetDefaultSampleFramesFromFreq(const int freq)
int *SDL_ChannelMapDup(const int *origchmap, int channels)
{
const size_t chmaplen = sizeof (*origchmap) * channels;
int *chmap = (int *) SDL_malloc(chmaplen);
int *chmap = (int *)SDL_malloc(chmaplen);
if (chmap) {
SDL_memcpy(chmap, origchmap, chmaplen);
}
@@ -544,8 +544,8 @@ static void DestroyPhysicalAudioDevice(SDL_AudioDevice *device)
SDL_DestroyMutex(device->lock);
SDL_DestroyCondition(device->close_cond);
SDL_free(device->work_buffer);
SDL_FreeLater(device->chmap); // this pointer is handed to the app during SDL_GetAudioDeviceChannelMap
SDL_FreeLater(device->name); // this pointer is handed to the app during SDL_GetAudioDeviceName
SDL_free(device->chmap);
SDL_free(device->name);
SDL_free(device);
}
@@ -1432,7 +1432,7 @@ const char *SDL_GetAudioDeviceName(SDL_AudioDeviceID devid)
const char *retval = NULL;
SDL_AudioDevice *device = ObtainPhysicalAudioDevice(devid);
if (device) {
retval = device->name;
retval = SDL_CreateTemporaryString(device->name);
}
ReleaseAudioDevice(device);
@@ -1465,8 +1465,8 @@ const int *SDL_GetAudioDeviceChannelMap(SDL_AudioDeviceID devid, int *count)
int channels = 0;
SDL_AudioDevice *device = ObtainPhysicalAudioDeviceDefaultAllowed(devid);
if (device) {
retval = device->chmap;
channels = device->spec.channels;
retval = SDL_FreeLater(SDL_ChannelMapDup(device->chmap, channels));
}
ReleaseAudioDevice(device);
+6 -6
View File
@@ -565,7 +565,7 @@ int SDL_SetAudioStreamFormat(SDL_AudioStream *stream, const SDL_AudioSpec *src_s
if (src_spec) {
if (src_spec->channels != stream->src_spec.channels) {
SDL_FreeLater(stream->src_chmap); // this pointer is handed to the app during SDL_GetAudioStreamInputChannelMap
SDL_free(stream->src_chmap);
stream->src_chmap = NULL;
}
SDL_copyp(&stream->src_spec, src_spec);
@@ -573,7 +573,7 @@ int SDL_SetAudioStreamFormat(SDL_AudioStream *stream, const SDL_AudioSpec *src_s
if (dst_spec) {
if (dst_spec->channels != stream->dst_spec.channels) {
SDL_FreeLater(stream->dst_chmap); // this pointer is handed to the app during SDL_GetAudioStreamInputChannelMap
SDL_free(stream->dst_chmap);
stream->dst_chmap = NULL;
}
SDL_copyp(&stream->dst_spec, dst_spec);
@@ -613,11 +613,11 @@ static int SetAudioStreamChannelMap(SDL_AudioStream *stream, const SDL_AudioSpec
if (!dupmap) {
retval = SDL_SetError("Invalid channel mapping");
} else {
SDL_FreeLater(*stream_chmap); // this pointer is handed to the app during SDL_GetAudioStreamInputChannelMap
SDL_free(*stream_chmap);
*stream_chmap = dupmap;
}
} else {
SDL_FreeLater(*stream_chmap); // this pointer is handed to the app during SDL_GetAudioStreamInputChannelMap
SDL_free(*stream_chmap);
*stream_chmap = NULL;
}
}
@@ -642,8 +642,8 @@ const int *SDL_GetAudioStreamInputChannelMap(SDL_AudioStream *stream, int *count
int channels = 0;
if (stream) {
SDL_LockMutex(stream->lock);
retval = stream->src_chmap;
channels = stream->src_spec.channels;
retval = SDL_FreeLater(SDL_ChannelMapDup(stream->src_chmap, channels));
SDL_UnlockMutex(stream->lock);
}
@@ -660,8 +660,8 @@ const int *SDL_GetAudioStreamOutputChannelMap(SDL_AudioStream *stream, int *coun
int channels = 0;
if (stream) {
SDL_LockMutex(stream->lock);
retval = stream->dst_chmap;
channels = stream->dst_spec.channels;
retval = SDL_FreeLater(SDL_ChannelMapDup(stream->dst_chmap, channels));
SDL_UnlockMutex(stream->lock);
}