Skip to content

Commit 70b2d16

Browse files
smcvslouken
authored andcommitted
audio: Assert that all devices from device_hash are the appropriate type
The keys and values of device_hash are pairs `(SDL_AudioDeviceID devid, void *dev)` where dev can be either a `SDL_AudioDevice *` or a `SDL_LogicalAudioDevice *`, depending on bit 1 of devid. We can confirm that we have got this right by looking at the instance_id member, because logical audio devices happen to start with the devid, whereas physical devices start with a pointer which is unlikely to match the devid by chance. Signed-off-by: Simon McVittie <[email protected]>
1 parent 83d4dce commit 70b2d16

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/audio/SDL_audio.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ static SDL_LogicalAudioDevice *ObtainLogicalAudioDevice(SDL_AudioDeviceID devid,
410410
SDL_LockRWLockForReading(current_audio.device_hash_lock);
411411
SDL_FindInHashTable(current_audio.device_hash, (const void *) (uintptr_t) devid, (const void **) &logdev);
412412
if (logdev) {
413+
SDL_assert(logdev->instance_id == devid);
413414
device = logdev->physical_device;
414415
SDL_assert(device != NULL);
415416
RefPhysicalAudioDevice(device); // reference it, in case the logical device migrates to a new default.
@@ -459,6 +460,7 @@ static SDL_AudioDevice *ObtainPhysicalAudioDevice(SDL_AudioDeviceID devid) // !
459460
} else {
460461
SDL_LockRWLockForReading(current_audio.device_hash_lock);
461462
SDL_FindInHashTable(current_audio.device_hash, (const void *) (uintptr_t) devid, (const void **) &device);
463+
SDL_assert(device->instance_id == devid);
462464
SDL_UnlockRWLock(current_audio.device_hash_lock);
463465

464466
if (!device) {
@@ -883,6 +885,7 @@ static bool SDLCALL FindLowestDeviceID(void *userdata, const SDL_HashTable *tabl
883885
if (isphysical && (devid_recording == data->recording) && (devid < data->highest)) {
884886
data->highest = devid;
885887
data->result = (SDL_AudioDevice *) value;
888+
SDL_assert(data->result->instance_id == devid);
886889
}
887890
return true; // keep iterating.
888891
}
@@ -1051,7 +1054,10 @@ static bool SDLCALL DestroyOnePhysicalAudioDevice(void *userdata, const SDL_Hash
10511054
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
10521055
const bool isphysical = !!(devid & (1<<1));
10531056
if (isphysical) {
1054-
DestroyPhysicalAudioDevice((SDL_AudioDevice *) value);
1057+
SDL_AudioDevice *dev = (SDL_AudioDevice *) value;
1058+
1059+
SDL_assert(dev->instance_id == devid);
1060+
DestroyPhysicalAudioDevice(dev);
10551061
}
10561062
return true; // keep iterating.
10571063
}
@@ -1485,6 +1491,7 @@ static bool SDLCALL FindAudioDeviceByCallback(void *userdata, const SDL_HashTabl
14851491
SDL_AudioDevice *device = (SDL_AudioDevice *) value;
14861492
if (data->callback(device, data->userdata)) { // found it?
14871493
data->retval = device;
1494+
SDL_assert(data->retval->instance_id == devid);
14881495
return false; // stop iterating, we found it.
14891496
}
14901497
}
@@ -1541,9 +1548,11 @@ const char *SDL_GetAudioDeviceName(SDL_AudioDeviceID devid)
15411548
SDL_SetError("Invalid audio device instance ID");
15421549
} else if (islogical) {
15431550
const SDL_LogicalAudioDevice *logdev = (const SDL_LogicalAudioDevice *) vdev;
1551+
SDL_assert(logdev->instance_id == devid);
15441552
result = SDL_GetPersistentString(logdev->physical_device->name);
15451553
} else {
15461554
const SDL_AudioDevice *device = (const SDL_AudioDevice *) vdev;
1555+
SDL_assert(device->instance_id == devid);
15471556
result = SDL_GetPersistentString(device->name);
15481557
}
15491558
SDL_UnlockRWLock(current_audio.device_hash_lock);

0 commit comments

Comments
 (0)