Fixed bug 3322 - Missing error checking in testaudioinfo and testaudiohotplug

Simon Hug

The two tests test/testaudioinfo.c and test/testaudiohotplug.c are missing error checking when they call SDL_GetAudioDeviceName. This function can return NULL which the tests pass straight to SDL_Log.
This commit is contained in:
Sam Lantinga 2016-10-01 12:29:55 -07:00
parent 6f11545a2d
commit 2cbe9e2b77
2 changed files with 32 additions and 8 deletions

View file

@ -74,6 +74,12 @@ poked(int sig)
done = 1; done = 1;
} }
static const char*
devtypestr(int iscapture)
{
return iscapture ? "capture" : "output";
}
static void static void
iteration() iteration()
{ {
@ -82,10 +88,21 @@ iteration()
while (SDL_PollEvent(&e)) { while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) { if (e.type == SDL_QUIT) {
done = 1; done = 1;
} else if (e.type == SDL_KEYUP) {
if (e.key.keysym.sym == SDLK_ESCAPE)
done = 1;
} else if (e.type == SDL_AUDIODEVICEADDED) { } else if (e.type == SDL_AUDIODEVICEADDED) {
const char *name = SDL_GetAudioDeviceName(e.adevice.which, 0); int index = e.adevice.which;
SDL_Log("New %s audio device: %s\n", e.adevice.iscapture ? "capture" : "output", name); int iscapture = e.adevice.iscapture;
if (!e.adevice.iscapture) { const char *name = SDL_GetAudioDeviceName(index, iscapture);
if (name != NULL)
SDL_Log("New %s audio device at index %u: %s\n", devtypestr(iscapture), (unsigned int) index, name);
else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Got new %s device at index %u, but failed to get the name: %s\n",
devtypestr(iscapture), (unsigned int) index, SDL_GetError());
continue;
}
if (!iscapture) {
positions[posindex] = 0; positions[posindex] = 0;
spec.userdata = &positions[posindex++]; spec.userdata = &positions[posindex++];
spec.callback = fillerup; spec.callback = fillerup;
@ -99,7 +116,7 @@ iteration()
} }
} else if (e.type == SDL_AUDIODEVICEREMOVED) { } else if (e.type == SDL_AUDIODEVICEREMOVED) {
dev = (SDL_AudioDeviceID) e.adevice.which; dev = (SDL_AudioDeviceID) e.adevice.which;
SDL_Log("%s device %u removed.\n", e.adevice.iscapture ? "capture" : "output", (unsigned int) dev); SDL_Log("%s device %u removed.\n", devtypestr(e.adevice.iscapture), (unsigned int) dev);
SDL_CloseAudioDevice(dev); SDL_CloseAudioDevice(dev);
} }
} }
@ -163,6 +180,7 @@ main(int argc, char *argv[])
SDL_Log("%i: %s", i, SDL_GetAudioDriver(i)); SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
} }
SDL_Log("Select a driver with the SDL_AUDIODRIVER environment variable.\n");
SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver()); SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
@ -175,6 +193,8 @@ main(int argc, char *argv[])
#endif #endif
/* Clean up on signal */ /* Clean up on signal */
/* Quit audio first, then free WAV. This prevents access violations in the audio threads. */
SDL_QuitSubSystem(SDL_INIT_AUDIO);
SDL_FreeWAV(sound); SDL_FreeWAV(sound);
SDL_Quit(); SDL_Quit();
return (0); return (0);

View file

@ -18,7 +18,7 @@ print_devices(int iscapture)
const char *typestr = ((iscapture) ? "capture" : "output"); const char *typestr = ((iscapture) ? "capture" : "output");
int n = SDL_GetNumAudioDevices(iscapture); int n = SDL_GetNumAudioDevices(iscapture);
SDL_Log("%s devices:\n", typestr); SDL_Log("Found %d %s device%s:\n", n, typestr, n != 1 ? "s" : "");
if (n == -1) if (n == -1)
SDL_Log(" Driver can't detect specific %s devices.\n\n", typestr); SDL_Log(" Driver can't detect specific %s devices.\n\n", typestr);
@ -27,7 +27,11 @@ print_devices(int iscapture)
else { else {
int i; int i;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
SDL_Log(" %s\n", SDL_GetAudioDeviceName(i, iscapture)); const char *name = SDL_GetAudioDeviceName(i, iscapture);
if (name != NULL)
SDL_Log(" %d: %s\n", i, name);
else
SDL_Log(" %d Error: %s\n", i, SDL_GetError());
} }
SDL_Log("\n"); SDL_Log("\n");
} }
@ -55,9 +59,9 @@ main(int argc, char **argv)
int i; int i;
SDL_Log("Built-in audio drivers:\n"); SDL_Log("Built-in audio drivers:\n");
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
SDL_Log(" %s\n", SDL_GetAudioDriver(i)); SDL_Log(" %d: %s\n", i, SDL_GetAudioDriver(i));
} }
SDL_Log("\n"); SDL_Log("Select a driver with the SDL_AUDIODRIVER environment variable.\n");
} }
SDL_Log("Using audio driver: %s\n\n", SDL_GetCurrentAudioDriver()); SDL_Log("Using audio driver: %s\n\n", SDL_GetCurrentAudioDriver());