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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
diff --git a/test/testaudiohotplug.c b/test/testaudiohotplug.c
index e13868e..73d4805 100644
--- a/test/testaudiohotplug.c
+++ b/test/testaudiohotplug.c
@@ -74,6 +74,12 @@ poked(int sig)
done = 1;
}
+static const char*
+devtypestr(int iscapture)
+{
+ return iscapture ? "capture" : "output";
+}
+
static void
iteration()
{
@@ -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;
@@ -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);
}
}
@@ -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__
@@ -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);
diff --git a/test/testaudioinfo.c b/test/testaudioinfo.c
index 53bf0f5..485fd0a 100644
--- a/test/testaudioinfo.c
+++ b/test/testaudioinfo.c
@@ -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);
@@ -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");
}
@@ -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());