Make sure count is 0 in out of memory conditions

This prevents a crash if the caller assumes that they can always dereference the returned pointer if there is a non-zero count.
This commit is contained in:
Sam Lantinga 2023-01-31 10:04:51 -08:00
parent 47deebe23f
commit c5f570b30b
2 changed files with 16 additions and 8 deletions

View file

@ -115,12 +115,12 @@ SDL_SensorID *SDL_GetSensors(int *count)
total_sensors += SDL_sensor_drivers[i]->GetCount();
}
if (count) {
*count = total_sensors;
}
sensors = (SDL_SensorID *)SDL_malloc((total_sensors + 1) * sizeof(*sensors));
if (sensors) {
if (count) {
*count = total_sensors;
}
for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
num_sensors = SDL_sensor_drivers[i]->GetCount();
for (device_index = 0; device_index < num_sensors; ++device_index) {
@ -133,6 +133,10 @@ SDL_SensorID *SDL_GetSensors(int *count)
SDL_assert(sensor_index == total_sensors);
sensors[sensor_index] = 0;
} else {
if (count) {
*count = 0;
}
SDL_OutOfMemory();
}
}