Fixed bug 3322 - Missing error checking in testaudioinfo and testaudiohotplug
authorSam Lantinga <slouken@libsdl.org>
Sat, 01 Oct 2016 12:29:55 -0700
changeset 10413636499ff46f8
parent 10412 a644712b2fb2
child 10414 92dc590a7116
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.
test/testaudiohotplug.c
test/testaudioinfo.c
     1.1 --- a/test/testaudiohotplug.c	Sat Oct 01 12:28:05 2016 -0700
     1.2 +++ b/test/testaudiohotplug.c	Sat Oct 01 12:29:55 2016 -0700
     1.3 @@ -74,6 +74,12 @@
     1.4      done = 1;
     1.5  }
     1.6  
     1.7 +static const char*
     1.8 +devtypestr(int iscapture)
     1.9 +{
    1.10 +    return iscapture ? "capture" : "output";
    1.11 +}
    1.12 +
    1.13  static void
    1.14  iteration()
    1.15  {
    1.16 @@ -82,10 +88,21 @@
    1.17      while (SDL_PollEvent(&e)) {
    1.18          if (e.type == SDL_QUIT) {
    1.19              done = 1;
    1.20 +        } else if (e.type == SDL_KEYUP) {
    1.21 +            if (e.key.keysym.sym == SDLK_ESCAPE)
    1.22 +                done = 1;
    1.23          } else if (e.type == SDL_AUDIODEVICEADDED) {
    1.24 -            const char *name = SDL_GetAudioDeviceName(e.adevice.which, 0);
    1.25 -            SDL_Log("New %s audio device: %s\n", e.adevice.iscapture ? "capture" : "output", name);
    1.26 -            if (!e.adevice.iscapture) {
    1.27 +            int index = e.adevice.which;
    1.28 +            int iscapture = e.adevice.iscapture;
    1.29 +            const char *name = SDL_GetAudioDeviceName(index, iscapture);
    1.30 +            if (name != NULL)
    1.31 +                SDL_Log("New %s audio device at index %u: %s\n", devtypestr(iscapture), (unsigned int) index, name);
    1.32 +            else {
    1.33 +                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Got new %s device at index %u, but failed to get the name: %s\n",
    1.34 +                    devtypestr(iscapture), (unsigned int) index, SDL_GetError());
    1.35 +                continue;
    1.36 +            }
    1.37 +            if (!iscapture) {
    1.38                  positions[posindex] = 0;
    1.39                  spec.userdata = &positions[posindex++];
    1.40                  spec.callback = fillerup;
    1.41 @@ -99,7 +116,7 @@
    1.42              }
    1.43          } else if (e.type == SDL_AUDIODEVICEREMOVED) {
    1.44              dev = (SDL_AudioDeviceID) e.adevice.which;
    1.45 -            SDL_Log("%s device %u removed.\n", e.adevice.iscapture ? "capture" : "output", (unsigned int) dev);
    1.46 +            SDL_Log("%s device %u removed.\n", devtypestr(e.adevice.iscapture), (unsigned int) dev);
    1.47              SDL_CloseAudioDevice(dev);
    1.48          }
    1.49      }
    1.50 @@ -163,6 +180,7 @@
    1.51          SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
    1.52      }
    1.53  
    1.54 +    SDL_Log("Select a driver with the SDL_AUDIODRIVER environment variable.\n");
    1.55      SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
    1.56  
    1.57  #ifdef __EMSCRIPTEN__
    1.58 @@ -175,6 +193,8 @@
    1.59  #endif
    1.60  
    1.61      /* Clean up on signal */
    1.62 +    /* Quit audio first, then free WAV. This prevents access violations in the audio threads. */
    1.63 +    SDL_QuitSubSystem(SDL_INIT_AUDIO);
    1.64      SDL_FreeWAV(sound);
    1.65      SDL_Quit();
    1.66      return (0);
     2.1 --- a/test/testaudioinfo.c	Sat Oct 01 12:28:05 2016 -0700
     2.2 +++ b/test/testaudioinfo.c	Sat Oct 01 12:29:55 2016 -0700
     2.3 @@ -18,7 +18,7 @@
     2.4      const char *typestr = ((iscapture) ? "capture" : "output");
     2.5      int n = SDL_GetNumAudioDevices(iscapture);
     2.6  
     2.7 -    SDL_Log("%s devices:\n", typestr);
     2.8 +    SDL_Log("Found %d %s device%s:\n", n, typestr, n != 1 ? "s" : "");
     2.9  
    2.10      if (n == -1)
    2.11          SDL_Log("  Driver can't detect specific %s devices.\n\n", typestr);
    2.12 @@ -27,7 +27,11 @@
    2.13      else {
    2.14          int i;
    2.15          for (i = 0; i < n; i++) {
    2.16 -            SDL_Log("  %s\n", SDL_GetAudioDeviceName(i, iscapture));
    2.17 +            const char *name = SDL_GetAudioDeviceName(i, iscapture);
    2.18 +            if (name != NULL)
    2.19 +                SDL_Log("  %d: %s\n", i, name);
    2.20 +            else
    2.21 +                SDL_Log("  %d Error: %s\n", i, SDL_GetError());
    2.22          }
    2.23          SDL_Log("\n");
    2.24      }
    2.25 @@ -55,9 +59,9 @@
    2.26          int i;
    2.27          SDL_Log("Built-in audio drivers:\n");
    2.28          for (i = 0; i < n; ++i) {
    2.29 -            SDL_Log("  %s\n", SDL_GetAudioDriver(i));
    2.30 +            SDL_Log("  %d: %s\n", i, SDL_GetAudioDriver(i));
    2.31          }
    2.32 -        SDL_Log("\n");
    2.33 +        SDL_Log("Select a driver with the SDL_AUDIODRIVER environment variable.\n");
    2.34      }
    2.35  
    2.36      SDL_Log("Using audio driver: %s\n\n", SDL_GetCurrentAudioDriver());