Skip to content

Commit

Permalink
Fixed bug 3322 - Missing error checking in testaudioinfo and testaudi…
Browse files Browse the repository at this point in the history
…ohotplug

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.
  • Loading branch information
slouken committed Oct 1, 2016
1 parent 6f11545 commit 2cbe9e2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
28 changes: 24 additions & 4 deletions test/testaudiohotplug.c
Expand Up @@ -74,6 +74,12 @@ poked(int sig)
done = 1;
}

static const char*
devtypestr(int iscapture)
{
return iscapture ? "capture" : "output";
}

static void
iteration()
{
Expand All @@ -82,10 +88,21 @@ iteration()
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
done = 1;
} else if (e.type == SDL_KEYUP) {
if (e.key.keysym.sym == SDLK_ESCAPE)
done = 1;
} else if (e.type == SDL_AUDIODEVICEADDED) {
const char *name = SDL_GetAudioDeviceName(e.adevice.which, 0);
SDL_Log("New %s audio device: %s\n", e.adevice.iscapture ? "capture" : "output", name);
if (!e.adevice.iscapture) {
int index = e.adevice.which;
int iscapture = 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;
spec.userdata = &positions[posindex++];
spec.callback = fillerup;
Expand All @@ -99,7 +116,7 @@ iteration()
}
} else if (e.type == SDL_AUDIODEVICEREMOVED) {
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);
}
}
Expand Down Expand Up @@ -163,6 +180,7 @@ main(int argc, char *argv[])
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());

#ifdef __EMSCRIPTEN__
Expand All @@ -175,6 +193,8 @@ main(int argc, char *argv[])
#endif

/* 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_Quit();
return (0);
Expand Down
12 changes: 8 additions & 4 deletions test/testaudioinfo.c
Expand Up @@ -18,7 +18,7 @@ print_devices(int iscapture)
const char *typestr = ((iscapture) ? "capture" : "output");
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)
SDL_Log(" Driver can't detect specific %s devices.\n\n", typestr);
Expand All @@ -27,7 +27,11 @@ print_devices(int iscapture)
else {
int 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");
}
Expand Down Expand Up @@ -55,9 +59,9 @@ main(int argc, char **argv)
int i;
SDL_Log("Built-in audio drivers:\n");
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());
Expand Down

0 comments on commit 2cbe9e2

Please sign in to comment.