SDL: cleanup WASAPI_PrepDevice

From f91211eb1700a53bcacdad73768c35672a446958 Mon Sep 17 00:00:00 2001
From: pionere <[EMAIL REDACTED]>
Date: Wed, 19 Jan 2022 14:51:42 +0100
Subject: [PATCH] cleanup WASAPI_PrepDevice - reorganize the loop which checks
 for the right wave-format - use the return value of UpdateAudioStream -
 ensure SetError is called in SDL_NewAudioStream

---
 src/audio/SDL_audiocvt.c      |  1 +
 src/audio/wasapi/SDL_wasapi.c | 15 +++++----------
 2 files changed, 6 insertions(+), 10 deletions(-)

diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c
index 147a9d134c4..7db64f5916a 100644
--- a/src/audio/SDL_audiocvt.c
+++ b/src/audio/SDL_audiocvt.c
@@ -1664,6 +1664,7 @@ SDL_NewAudioStream(const SDL_AudioFormat src_format,
 
     retval = (SDL_AudioStream *) SDL_calloc(1, sizeof (SDL_AudioStream));
     if (!retval) {
+        SDL_OutOfMemory();
         return NULL;
     }
 
diff --git a/src/audio/wasapi/SDL_wasapi.c b/src/audio/wasapi/SDL_wasapi.c
index bfb82f0b177..b556f58547b 100644
--- a/src/audio/wasapi/SDL_wasapi.c
+++ b/src/audio/wasapi/SDL_wasapi.c
@@ -209,7 +209,7 @@ UpdateAudioStream(_THIS, const SDL_AudioSpec *oldspec)
         }
 
         if (!this->stream) {
-            return -1;
+            return -1; /* SDL_NewAudioStream should have called SDL_SetError. */
         }
     }
 
@@ -512,9 +512,8 @@ WASAPI_PrepDevice(_THIS, const SDL_bool updatestream)
     IAudioRenderClient *render = NULL;
     IAudioCaptureClient *capture = NULL;
     WAVEFORMATEX *waveformat = NULL;
-    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
+    SDL_AudioFormat test_format;
     SDL_AudioFormat wasapi_format = 0;
-    SDL_bool valid_format = SDL_FALSE;
     HRESULT ret = S_OK;
     DWORD streamflags = 0;
 
@@ -543,16 +542,14 @@ WASAPI_PrepDevice(_THIS, const SDL_bool updatestream)
     /* Make sure we have a valid format that we can convert to whatever WASAPI wants. */
     wasapi_format = WaveFormatToSDLFormat(waveformat);
 
-    while ((!valid_format) && (test_format)) {
+    for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
         if (test_format == wasapi_format) {
             this->spec.format = test_format;
-            valid_format = SDL_TRUE;
             break;
         }
-        test_format = SDL_NextAudioFormat();
     }
 
-    if (!valid_format) {
+    if (!test_format) {
         return SDL_SetError("WASAPI: Unsupported audio format");
     }
 
@@ -631,9 +628,7 @@ WASAPI_PrepDevice(_THIS, const SDL_bool updatestream)
     }
 
     if (updatestream) {
-        if (UpdateAudioStream(this, &oldspec) == -1) {
-            return -1;
-        }
+        return UpdateAudioStream(this, &oldspec);
     }
 
     return 0;  /* good to go. */