SDL: Clang-Tidy fixes (#6725)

From 3c501b963dd8f0605a6ce7978882df39ba76f9cd Mon Sep 17 00:00:00 2001
From: Pierre Wendling <[EMAIL REDACTED]>
Date: Thu, 1 Dec 2022 16:07:03 -0500
Subject: [PATCH] Clang-Tidy fixes (#6725)

---
 include/SDL3/SDL_audio.h                      |  12 +-
 include/SDL3/SDL_mutex.h                      |   4 +-
 include/SDL3/SDL_power.h                      |   7 +-
 include/SDL3/SDL_surface.h                    |  16 +-
 src/SDL_assert.c                              |   4 +-
 src/SDL_error.c                               |   2 +-
 src/SDL_log.c                                 |  22 ++-
 src/audio/SDL_audio.c                         |  59 ++++--
 src/audio/SDL_audiocvt.c                      |  45 ++---
 src/audio/SDL_audiodev.c                      |  11 +-
 src/audio/SDL_audiotypecvt.c                  |   2 +-
 src/audio/SDL_wave.c                          |   8 +-
 src/audio/alsa/SDL_alsa_audio.c               |  19 +-
 src/audio/coreaudio/SDL_coreaudio.m           |  40 ++--
 src/audio/dsp/SDL_dspaudio.c                  |   4 +-
 src/audio/jack/SDL_jackaudio.c                |  14 +-
 src/audio/pipewire/SDL_pipewire.c             |  23 +--
 src/audio/sndio/SDL_sndioaudio.c              |  35 ++--
 src/core/android/SDL_android.c                |   1 -
 src/core/linux/SDL_evdev.c                    |  12 +-
 src/core/linux/SDL_evdev_capabilities.c       |   8 +-
 src/core/linux/SDL_evdev_capabilities.h       |   8 +-
 src/core/linux/SDL_evdev_kbd.c                |  98 +++++-----
 src/core/linux/SDL_fcitx.c                    |   4 +-
 src/core/linux/SDL_ibus.c                     |  10 +-
 src/core/windows/SDL_windows.c                |   8 +-
 src/events/SDL_events.c                       | 180 +++++++++---------
 src/events/SDL_keyboard.c                     |   7 +-
 src/events/SDL_keyboard_c.h                   |   2 +-
 src/events/SDL_mouse.c                        |   2 +-
 src/events/SDL_quit.c                         |   2 +-
 src/file/SDL_rwops.c                          |   2 +-
 src/file/cocoa/SDL_rwopsbundlesupport.m       |   2 +-
 src/filesystem/unix/SDL_sysfilesystem.c       |   4 +-
 src/haptic/SDL_haptic.c                       |   9 +-
 src/haptic/android/SDL_syshaptic.c            |   5 +-
 src/haptic/darwin/SDL_syshaptic.c             |   3 +-
 src/haptic/linux/SDL_syshaptic.c              |   9 +-
 src/haptic/windows/SDL_dinputhaptic.c         |  41 ++--
 src/haptic/windows/SDL_xinputhaptic.c         |   4 +-
 src/hidapi/SDL_hidapi.c                       |  60 +++---
 src/joystick/SDL_gamecontroller.c             |  76 ++++----
 src/joystick/SDL_joystick.c                   |   6 +-
 src/joystick/apple/SDL_mfijoystick.m          |  31 +--
 src/joystick/darwin/SDL_iokitjoystick.c       |   5 +-
 src/joystick/hidapi/SDL_hidapi_gamecube.c     |   2 +-
 src/joystick/hidapi/SDL_hidapi_ps3.c          |  11 +-
 src/joystick/hidapi/SDL_hidapi_ps4.c          |  20 +-
 src/joystick/hidapi/SDL_hidapi_ps5.c          |  12 +-
 src/joystick/hidapi/SDL_hidapi_shield.c       |   2 +-
 src/joystick/hidapi/SDL_hidapi_steam.c        |  27 +--
 src/joystick/hidapi/SDL_hidapi_switch.c       |  29 +--
 src/joystick/hidapi/SDL_hidapi_xboxone.c      |  10 +-
 src/joystick/hidapi/SDL_hidapijoystick.c      |  10 +-
 src/joystick/linux/SDL_sysjoystick.c          |   6 +-
 src/joystick/virtual/SDL_virtualjoystick.c    |   1 -
 src/joystick/windows/SDL_dinputjoystick.c     |  14 +-
 src/joystick/windows/SDL_rawinputjoystick.c   |   8 +-
 .../windows/SDL_windows_gaming_input.c        |   6 +-
 src/joystick/windows/SDL_windowsjoystick.c    |   6 +-
 src/joystick/windows/SDL_xinputjoystick.c     |  28 +--
 src/locale/macos/SDL_syslocale.m              |   3 +-
 src/locale/windows/SDL_syslocale.c            |   2 +-
 src/main/windows/SDL_windows_main.c           |   4 +-
 src/misc/SDL_url.c                            |   2 -
 src/power/linux/SDL_syspower.c                |   2 +-
 src/render/SDL_render.c                       |  20 +-
 src/render/SDL_yuv_sw.c                       |   8 +-
 src/render/direct3d/SDL_render_d3d.c          |  29 +--
 src/render/direct3d11/SDL_render_d3d11.c      |  16 +-
 src/render/direct3d12/SDL_render_d3d12.c      |  18 +-
 src/render/metal/SDL_render_metal.m           |  45 +++--
 src/render/opengl/SDL_render_gl.c             |  13 +-
 src/render/opengl/SDL_shaders_gl.c            |   2 +-
 src/render/opengles2/SDL_render_gles2.c       |  13 +-
 src/render/software/SDL_render_sw.c           |   8 +-
 src/render/software/SDL_rotate.c              |   2 +-
 src/render/software/SDL_triangle.c            |  27 ++-
 src/stdlib/SDL_mslibc.c                       |   4 +
 src/stdlib/SDL_string.c                       |  21 +-
 src/test/SDL_test_assert.c                    |   6 +-
 src/test/SDL_test_common.c                    | 100 +++++-----
 src/test/SDL_test_compare.c                   |  10 +-
 src/test/SDL_test_font.c                      |   2 +-
 src/test/SDL_test_harness.c                   |   6 +-
 src/test/SDL_test_log.c                       |  13 +-
 src/test/SDL_test_memory.c                    |  10 +-
 src/thread/SDL_thread.c                       |  10 +-
 src/thread/pthread/SDL_syscond.c              |   2 +-
 src/video/SDL_RLEaccel.c                      |  60 +++---
 src/video/SDL_blit.c                          |   2 +-
 src/video/SDL_blit_A.c                        |  12 +-
 src/video/SDL_blit_N.c                        |   6 +-
 src/video/SDL_bmp.c                           |  73 +++----
 src/video/SDL_egl.c                           |   2 +-
 src/video/SDL_fillrect.c                      |   2 +-
 src/video/SDL_pixels.c                        |  12 +-
 src/video/SDL_shape.c                         |  31 ++-
 src/video/SDL_video.c                         |  25 ++-
 src/video/SDL_vulkan_utils.c                  |   4 +-
 src/video/SDL_yuv.c                           |  12 +-
 src/video/android/SDL_androidmouse.c          |   2 +-
 src/video/cocoa/SDL_cocoaevents.m             |  10 +-
 src/video/cocoa/SDL_cocoakeyboard.m           |   5 +-
 src/video/cocoa/SDL_cocoametalview.m          |   3 +-
 src/video/cocoa/SDL_cocoamodes.h              |   2 +-
 src/video/cocoa/SDL_cocoavulkan.m             |   4 +-
 src/video/cocoa/SDL_cocoawindow.m             |   6 +-
 src/video/dummy/SDL_nullframebuffer.c         |   4 +-
 src/video/kmsdrm/SDL_kmsdrmdyn.c              |   3 -
 src/video/kmsdrm/SDL_kmsdrmmouse.c            |   4 +-
 src/video/kmsdrm/SDL_kmsdrmvideo.c            |  16 +-
 .../offscreen/SDL_offscreenframebuffer.c      |   4 +-
 src/video/wayland/SDL_waylanddatamanager.c    |  33 ++--
 src/video/wayland/SDL_waylandevents.c         |  14 +-
 src/video/wayland/SDL_waylandmessagebox.c     |   2 +-
 src/video/wayland/SDL_waylandvideo.c          |   9 +-
 src/video/windows/SDL_windowsevents.c         |  17 +-
 src/video/windows/SDL_windowsframebuffer.c    |   2 +-
 src/video/windows/SDL_windowskeyboard.c       |  31 +--
 src/video/windows/SDL_windowsmessagebox.c     |   8 +-
 src/video/windows/SDL_windowsmodes.c          |   1 +
 src/video/windows/SDL_windowsmouse.c          |   4 +-
 src/video/windows/SDL_windowsshape.c          |   3 +-
 src/video/windows/SDL_windowsvulkan.c         |   2 +-
 src/video/windows/SDL_windowswindow.c         |  20 +-
 src/video/windows/SDL_windowswindow.h         |   4 +-
 src/video/x11/SDL_x11events.c                 |   8 +-
 src/video/x11/SDL_x11framebuffer.c            |  11 +-
 src/video/x11/SDL_x11keyboard.c               |  13 +-
 src/video/x11/SDL_x11messagebox.c             |   7 +-
 src/video/x11/SDL_x11modes.c                  |   2 +-
 src/video/x11/SDL_x11mouse.c                  |  10 +-
 src/video/x11/SDL_x11shape.h                  |   2 +-
 src/video/x11/SDL_x11video.c                  |   5 +-
 src/video/x11/SDL_x11window.c                 |  16 +-
 src/video/x11/SDL_x11window.h                 |   2 +-
 test/checkkeys.c                              |   2 +-
 test/checkkeysthreads.c                       |  28 ++-
 test/controllermap.c                          |  13 +-
 test/loopwavequeue.c                          |   8 +-
 test/testatomic.c                             |  12 +-
 test/testaudiohotplug.c                       |  12 +-
 test/testaudioinfo.c                          |  11 +-
 test/testautomation.c                         |   2 +-
 test/testautomation_audio.c                   |  14 +-
 test/testautomation_clipboard.c               |   8 +-
 test/testautomation_keyboard.c                |   4 +-
 test/testautomation_math.c                    |   2 +-
 test/testautomation_platform.c                |   8 +-
 test/testautomation_rect.c                    |  28 +--
 test/testautomation_rwops.c                   |  10 +-
 test/testautomation_sdltest.c                 |  55 +++---
 test/testautomation_stdlib.c                  |   2 +-
 test/testautomation_surface.c                 |   4 -
 test/testautomation_video.c                   |   6 +-
 test/testcustomcursor.c                       |   5 +-
 test/testdisplayinfo.c                        |   2 +-
 test/testfile.c                               |  16 +-
 test/testgamecontroller.c                     |   2 +-
 test/testgles2.c                              |   2 +-
 test/testiconv.c                              |   4 +-
 test/testime.c                                |  43 +++--
 test/testintersections.c                      |  16 +-
 test/testloadso.c                             |   2 -
 test/testlock.c                               |  20 +-
 test/testmultiaudio.c                         |   1 -
 test/testnative.c                             |   2 +-
 test/testoffscreen.c                          |   7 +-
 test/testoverlay2.c                           |   1 +
 test/testpower.c                              |   3 +-
 test/testqsort.c                              |   5 +-
 test/testresample.c                           |   2 +-
 test/testsem.c                                |  16 +-
 test/testsensor.c                             |   2 +-
 test/testshader.c                             |   6 +-
 test/testshape.c                              |   4 +-
 test/testthread.c                             |   4 +-
 test/testutils.c                              |   2 +-
 test/testvulkan.c                             |   4 +-
 test/testwm2.c                                |   8 +-
 test/testyuv.c                                |   2 +-
 test/testyuv_cvt.c                            |   2 +-
 test/torturethread.c                          |   6 +-
 184 files changed, 1311 insertions(+), 1153 deletions(-)

diff --git a/include/SDL3/SDL_audio.h b/include/SDL3/SDL_audio.h
index 73415d3c0bcb..ee6cea02c8d4 100644
--- a/include/SDL3/SDL_audio.h
+++ b/include/SDL3/SDL_audio.h
@@ -1008,12 +1008,12 @@ typedef struct _SDL_AudioStream SDL_AudioStream;
  * \sa SDL_AudioStreamClear
  * \sa SDL_FreeAudioStream
  */
-extern DECLSPEC SDL_AudioStream * SDLCALL SDL_NewAudioStream(const SDL_AudioFormat src_format,
-                                           const Uint8 src_channels,
-                                           const int src_rate,
-                                           const SDL_AudioFormat dst_format,
-                                           const Uint8 dst_channels,
-                                           const int dst_rate);
+extern DECLSPEC SDL_AudioStream *SDLCALL SDL_NewAudioStream(SDL_AudioFormat src_format,
+                                                            Uint8 src_channels,
+                                                            int src_rate,
+                                                            SDL_AudioFormat dst_format,
+                                                            Uint8 dst_channels,
+                                                            int dst_rate);
 
 /**
  * Add data to be converted/resampled to the stream.
diff --git a/include/SDL3/SDL_mutex.h b/include/SDL3/SDL_mutex.h
index d27463add889..6fdbd7db9a0c 100644
--- a/include/SDL3/SDL_mutex.h
+++ b/include/SDL3/SDL_mutex.h
@@ -276,7 +276,7 @@ extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
  * successful it will atomically decrement the semaphore value.
  *
  * \param sem the semaphore to wait on
- * \param ms the length of the timeout, in milliseconds
+ * \param timeout the length of the timeout, in milliseconds
  * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
  *          succeed in the allotted time, or a negative error code on failure;
  *          call SDL_GetError() for more information.
@@ -290,7 +290,7 @@ extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
  * \sa SDL_SemValue
  * \sa SDL_SemWait
  */
-extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
+extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout);
 
 /**
  * Atomically increment a semaphore's value and wake waiting threads.
diff --git a/include/SDL3/SDL_power.h b/include/SDL3/SDL_power.h
index 48884f370027..64db261dd9c1 100644
--- a/include/SDL3/SDL_power.h
+++ b/include/SDL3/SDL_power.h
@@ -48,7 +48,6 @@ typedef enum
     SDL_POWERSTATE_CHARGED       /**< Plugged in, battery charged */
 } SDL_PowerState;
 
-
 /**
  * Get the current power supply details.
  *
@@ -65,17 +64,17 @@ typedef enum
  * It's possible a platform can only report battery percentage or time left
  * but not both.
  *
- * \param secs seconds of battery life left, you can pass a NULL here if you
+ * \param seconds seconds of battery life left, you can pass a NULL here if you
  *             don't care, will return -1 if we can't determine a value, or
  *             we're not running on a battery
- * \param pct percentage of battery life left, between 0 and 100, you can pass
+ * \param percent percentage of battery life left, between 0 and 100, you can pass
  *            a NULL here if you don't care, will return -1 if we can't
  *            determine a value, or we're not running on a battery
  * \returns an SDL_PowerState enum representing the current battery state.
  *
  * \since This function is available since SDL 3.0.0.
  */
-extern DECLSPEC SDL_PowerState SDLCALL SDL_GetPowerInfo(int *secs, int *pct);
+extern DECLSPEC SDL_PowerState SDLCALL SDL_GetPowerInfo(int *seconds, int *percent);
 
 /* Ends C function definitions when using C++ */
 #ifdef __cplusplus
diff --git a/include/SDL3/SDL_surface.h b/include/SDL3/SDL_surface.h
index 32d833137c20..b768fea45de1 100644
--- a/include/SDL3/SDL_surface.h
+++ b/include/SDL3/SDL_surface.h
@@ -554,9 +554,9 @@ extern DECLSPEC SDL_Surface *SDLCALL SDL_DuplicateSurface(SDL_Surface * surface)
  * surface. The new, optimized surface can then be used as the source for
  * future blits, making them faster.
  *
- * \param src the existing SDL_Surface structure to convert
- * \param fmt the SDL_PixelFormat structure that the new surface is optimized
- *            for
+ * \param surface the existing SDL_Surface structure to convert
+ * \param format the SDL_PixelFormat structure that the new surface is
+ *               optimized for
  * \returns the new SDL_Surface structure that is created or NULL if it fails;
  *          call SDL_GetError() for more information.
  *
@@ -566,8 +566,8 @@ extern DECLSPEC SDL_Surface *SDLCALL SDL_DuplicateSurface(SDL_Surface * surface)
  * \sa SDL_ConvertSurfaceFormat
  * \sa SDL_CreateSurface
  */
-extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
-    (SDL_Surface * src, const SDL_PixelFormat * fmt);
+extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface(SDL_Surface *surface,
+                                                        const SDL_PixelFormat *format);
 
 /**
  * Copy an existing surface to a new surface of the specified format enum.
@@ -577,7 +577,7 @@ extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
  * it might be easier to call but it doesn't have access to palette
  * information for the destination surface, in case that would be important.
  *
- * \param src the existing SDL_Surface structure to convert
+ * \param surface the existing SDL_Surface structure to convert
  * \param pixel_format the SDL_PixelFormatEnum that the new surface is
  *                     optimized for
  * \returns the new SDL_Surface structure that is created or NULL if it fails;
@@ -589,8 +589,8 @@ extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
  * \sa SDL_ConvertSurface
  * \sa SDL_CreateSurface
  */
-extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurfaceFormat
-    (SDL_Surface * src, Uint32 pixel_format);
+extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurfaceFormat(SDL_Surface *surface,
+                                                              Uint32 pixel_format);
 
 /**
  * Copy a block of pixels of one format to another format.
diff --git a/src/SDL_assert.c b/src/SDL_assert.c
index bf0370175258..9e5947c09d57 100644
--- a/src/SDL_assert.c
+++ b/src/SDL_assert.c
@@ -280,8 +280,8 @@ static SDL_assert_state SDLCALL SDL_PromptAssertion(const SDL_assert_data *data,
         /* this is a little hacky. */
         for (;;) {
             char buf[32];
-            fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
-            fflush(stderr);
+            (void)fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
+            (void)fflush(stderr);
             if (fgets(buf, sizeof(buf), stdin) == NULL) {
                 break;
             }
diff --git a/src/SDL_error.c b/src/SDL_error.c
index 3b6c48e818e1..704ce29f2f44 100644
--- a/src/SDL_error.c
+++ b/src/SDL_error.c
@@ -45,7 +45,7 @@ int SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
                 error->str = str;
                 error->len = len;
                 va_start(ap, fmt);
-                SDL_vsnprintf(error->str, error->len, fmt, ap);
+                (void)SDL_vsnprintf(error->str, error->len, fmt, ap);
                 va_end(ap);
             }
         }
diff --git a/src/SDL_log.c b/src/SDL_log.c
index 86e3538dfd49..99d748954871 100644
--- a/src/SDL_log.c
+++ b/src/SDL_log.c
@@ -412,7 +412,7 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
 
         length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1 + 1;
         output = SDL_small_alloc(char, length, &isstack);
-        SDL_snprintf(output, length, "%s: %s\r\n", SDL_priority_prefixes[priority], message);
+        (void)SDL_snprintf(output, length, "%s: %s\r\n", SDL_priority_prefixes[priority], message);
         tstr = WIN_UTF8ToString(output);
 
         /* Output to debugger */
@@ -457,27 +457,33 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
     {
         FILE *pFile;
         pFile = fopen("SDL_Log.txt", "a");
-        fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
-        fclose(pFile);
+        if (pFile != NULL) {
+            (void)fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
+            (void)fclose(pFile);
+        }
     }
 #elif defined(__VITA__)
     {
         FILE *pFile;
         pFile = fopen("ux0:/data/SDL_Log.txt", "a");
-        fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
-        fclose(pFile);
+        if (pFile != NULL) {
+            (void)fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
+            (void)fclose(pFile);
+        }
     }
 #elif defined(__3DS__)
     {
         FILE *pFile;
         pFile = fopen("sdmc:/3ds/SDL_Log.txt", "a");
-        fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
-        fclose(pFile);
+        if (pFile != NULL) {
+            (void)fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
+            (void)fclose(pFile);
+        }
     }
 #endif
 #if HAVE_STDIO_H && \
     !(defined(__APPLE__) && (defined(SDL_VIDEO_DRIVER_COCOA) || defined(SDL_VIDEO_DRIVER_UIKIT)))
-    fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
+    (void)fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
 #else
     /* We won't print anything, but reference the priority prefix anyway
        to avoid a compiler warning.
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index fcf973ab653b..151b799768a5 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -362,7 +362,7 @@ static int add_audio_device(const char *name, SDL_AudioSpec *spec, void *handle,
             return SDL_OutOfMemory();
         }
 
-        SDL_snprintf(replacement, len, "%s (%d)", name, dupenum + 1);
+        (void)SDL_snprintf(replacement, len, "%s (%d)", name, dupenum + 1);
         item->dupenum = dupenum;
         item->name = replacement;
     }
@@ -626,6 +626,10 @@ void SDL_ClearQueuedAudio(SDL_AudioDeviceID devid)
     current_audio.impl.UnlockDevice(device);
 }
 
+#if SDL_AUDIO_DRIVER_ANDROID
+extern void Android_JNI_AudioSetThreadPriority(int, int);
+#endif
+
 /* The general mixing thread function */
 static int SDLCALL SDL_RunAudio(void *devicep)
 {
@@ -640,7 +644,6 @@ static int SDLCALL SDL_RunAudio(void *devicep)
 #if SDL_AUDIO_DRIVER_ANDROID
     {
         /* Set thread priority to THREAD_PRIORITY_AUDIO */
-        extern void Android_JNI_AudioSetThreadPriority(int, int);
         Android_JNI_AudioSetThreadPriority(device->iscapture, device->id);
     }
 #else
@@ -741,7 +744,6 @@ static int SDLCALL SDL_CaptureAudio(void *devicep)
 #if SDL_AUDIO_DRIVER_ANDROID
     {
         /* Set thread priority to THREAD_PRIORITY_AUDIO */
-        extern void Android_JNI_AudioSetThreadPriority(int, int);
         Android_JNI_AudioSetThreadPriority(device->iscapture, device->id);
     }
 #else
@@ -1155,6 +1157,19 @@ static void close_audio_device(SDL_AudioDevice *device)
     SDL_free(device);
 }
 
+static Uint16
+GetDefaultSamplesFromFreq(int freq)
+{
+    /* Pick a default of ~46 ms at desired frequency */
+    /* !!! FIXME: remove this when the non-Po2 resampling is in. */
+    const Uint16 max_sample = (freq / 1000) * 46;
+    Uint16 current_sample = 1;
+    while (current_sample < max_sample) {
+        current_sample *= 2;
+    }
+    return current_sample;
+}
+
 /*
  * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig).
  *  Fills in a sanitized copy in (prepared).
@@ -1165,23 +1180,33 @@ static int prepare_audiospec(const SDL_AudioSpec *orig, SDL_AudioSpec *prepared)
     SDL_copyp(prepared, orig);
 
     if (orig->freq == 0) {
+        static const int DEFAULT_FREQ = 22050;
         const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
-        if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) {
-            prepared->freq = 22050; /* a reasonable default */
+        if (env != NULL) {
+            int freq = SDL_atoi(env);
+            prepared->freq = freq != 0 ? freq : DEFAULT_FREQ;
+        } else {
+            prepared->freq = DEFAULT_FREQ;
         }
     }
 
     if (orig->format == 0) {
         const char *env = SDL_getenv("SDL_AUDIO_FORMAT");
-        if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) {
-            prepared->format = AUDIO_S16; /* a reasonable default */
+        if (env != NULL) {
+            const SDL_AudioFormat format = SDL_ParseAudioFormat(env);
+            prepared->format = format != 0 ? format : AUDIO_S16;
+        } else {
+            prepared->format = AUDIO_S16;
         }
     }
 
     if (orig->channels == 0) {
         const char *env = SDL_getenv("SDL_AUDIO_CHANNELS");
-        if ((!env) || ((prepared->channels = (Uint8)SDL_atoi(env)) == 0)) {
-            prepared->channels = 2; /* a reasonable default */
+        if (env != NULL) {
+            Uint8 channels = (Uint8)SDL_atoi(env);
+            prepared->channels = channels != 0 ? channels : 2;
+        } else {
+            prepared->channels = 2;
         }
     } else if (orig->channels > 8) {
         SDL_SetError("Unsupported number of audio channels.");
@@ -1190,15 +1215,11 @@ static int prepare_audiospec(const SDL_AudioSpec *orig, SDL_AudioSpec *prepared)
 
     if (orig->samples == 0) {
         const char *env = SDL_getenv("SDL_AUDIO_SAMPLES");
-        if ((!env) || ((prepared->samples = (Uint16)SDL_atoi(env)) == 0)) {
-            /* Pick a default of ~46 ms at desired frequency */
-            /* !!! FIXME: remove this when the non-Po2 resampling is in. */
-            const int samples = (prepared->freq / 1000) * 46;
-            int power2 = 1;
-            while (power2 < samples) {
-                power2 *= 2;
-            }
-            prepared->samples = power2;
+        if (env != NULL) {
+            Uint16 samples = (Uint16)SDL_atoi(env);
+            prepared->samples = samples != 0 ? samples : GetDefaultSamplesFromFreq(prepared->freq);
+        } else {
+            prepared->samples = GetDefaultSamplesFromFreq(prepared->freq);
         }
     }
 
@@ -1456,7 +1477,7 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
         const size_t stacksize = is_internal_thread ? 64 * 1024 : 0;
         char threadname[64];
 
-        SDL_snprintf(threadname, sizeof(threadname), "SDLAudio%c%d", (iscapture) ? 'C' : 'P', (int)device->id);
+        (void)SDL_snprintf(threadname, sizeof threadname, "SDLAudio%c%" SDL_PRIu32, (iscapture) ? 'C' : 'P', device->id);
         device->thread = SDL_CreateThreadInternal(iscapture ? SDL_CaptureAudio : SDL_RunAudio, threadname, stacksize, device);
 
         if (device->thread == NULL) {
diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c
index 5dabf6ce2396..03d2c0bba12d 100644
--- a/src/audio/SDL_audiocvt.c
+++ b/src/audio/SDL_audiocvt.c
@@ -204,7 +204,7 @@ static int SDL_ResampleAudio(const int chans, const int inrate, const int outrat
     const int paddinglen = ResamplerPadding(inrate, outrate);
     const int framelen = chans * (int)sizeof(float);
     const int inframes = inbuflen / framelen;
-    const int wantedoutframes = (int)((inbuflen / framelen) * ratio); /* outbuflen isn't total to write, it's total available. */
+    const int wantedoutframes = (int)(inframes * ratio); /* outbuflen isn't total to write, it's total available. */
     const int maxoutframes = outbuflen / framelen;
     const int outframes = SDL_min(wantedoutframes, maxoutframes);
     ResampleFloatType outtime = 0.0f;
@@ -229,7 +229,7 @@ static int SDL_ResampleAudio(const int chans, const int inrate, const int outrat
                 const int srcframe = srcindex - j;
                 /* !!! FIXME: we can bubble this conditional out of here by doing a pre loop. */
                 const float insample = (srcframe < 0) ? lpadding[((paddinglen + srcframe) * chans) + chan] : inbuf[(srcframe * chans) + chan];
-                outsample += (float)(insample * (ResamplerFilter[filterindex1 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)] + (interpolation1 * ResamplerFilterDifference[filterindex1 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)])));
+                outsample += (insample * (ResamplerFilter[filterindex1 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)] + (interpolation1 * ResamplerFilterDifference[filterindex1 + (j * RESAMPLER_SAMPLES_PER_ZERO_CROSSING)])));
             }
 
             /* Do the right wing! */
@@ -238,7 +238,7 @@ static int SDL_ResampleAudio(const int chans, const int inrate, const int outrat
                 const int srcframe = srcindex + 1 + j;
                 /* !!! FIXME: we can bubble this conditional out of here by doing a post loop. */
                 const float insample = (srcframe >= inframes) ? rpadding[((srcframe - inframes) * chans) + chan] : inbuf[(srcframe * chans) + chan];
-                outsample += (float)(insample * (ResamplerFilter[filterindex2 + jsamples] + (interpolation2 * ResamplerFilterDifference[filterindex2 + jsamples])));
+                outsample += (insample * (ResamplerFilter[filterindex2 + jsamples] + (interpolation2 * ResamplerFilterDifference[filterindex2 + jsamples])));
             }
 
             *(dst++) = outsample;
@@ -312,7 +312,7 @@ static void SDLCALL SDL_Convert_Byteswap(SDL_AudioCVT *cvt, SDL_AudioFormat form
     }
 }
 
-static int SDL_AddAudioCVTFilter(SDL_AudioCVT *cvt, const SDL_AudioFilter filter)
+static int SDL_AddAudioCVTFilter(SDL_AudioCVT *cvt, SDL_AudioFilter filter)
 {
     if (cvt->filter_index >= SDL_AUDIOCVT_MAX_FILTERS) {
         return SDL_SetError("Too many filters needed for conversion, exceeded maximum of %d", SDL_AUDIOCVT_MAX_FILTERS);
@@ -372,7 +372,8 @@ static int SDL_BuildAudioTypeCVTToFloat(SDL_AudioCVT *cvt, const SDL_AudioFormat
             cvt->len_mult *= mult;
             cvt->len_ratio *= mult;
         } else if (src_bitsize > dst_bitsize) {
-            cvt->len_ratio /= (src_bitsize / dst_bitsize);
+            const int div = (src_bitsize / dst_bitsize);
+            cvt->len_ratio /= div;
         }
 
         retval = 1; /* added a converter. */
@@ -670,8 +671,8 @@ static SDL_bool SDL_SupportedChannelCount(const int channels)
 */
 
 int SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
-                      SDL_AudioFormat src_fmt, Uint8 src_channels, int src_rate,
-                      SDL_AudioFormat dst_fmt, Uint8 dst_channels, int dst_rate)
+                      SDL_AudioFormat src_format, Uint8 src_channels, int src_rate,
+                      SDL_AudioFormat dst_format, Uint8 dst_channels, int dst_rate)
 {
     SDL_AudioFilter channel_converter = NULL;
 
@@ -683,10 +684,10 @@ int SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
     /* Make sure we zero out the audio conversion before error checking */
     SDL_zerop(cvt);
 
-    if (!SDL_SupportedAudioFormat(src_fmt)) {
+    if (!SDL_SupportedAudioFormat(src_format)) {
         return SDL_SetError("Invalid source format");
     }
-    if (!SDL_SupportedAudioFormat(dst_fmt)) {
+    if (!SDL_SupportedAudioFormat(dst_format)) {
         return SDL_SetError("Invalid destination format");
     }
     if (!SDL_SupportedChannelCount(src_channels)) {
@@ -710,12 +711,12 @@ int SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
 
 #if DEBUG_CONVERT
     SDL_Log("SDL_AUDIO_CONVERT: Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
-            src_fmt, dst_fmt, src_channels, dst_channels, src_rate, dst_rate);
+            src_format, dst_format, src_channels, dst_channels, src_rate, dst_rate);
 #endif
 
     /* Start off with no conversion necessary */
-    cvt->src_format = src_fmt;
-    cvt->dst_format = dst_fmt;
+    cvt->src_format = src_format;
+    cvt->dst_format = dst_format;
     cvt->needed = 0;
     cvt->filter_index = 0;
     SDL_zeroa(cvt->filters);
@@ -742,13 +743,13 @@ int SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
 
     /* see if we can skip float conversion entirely. */
     if (src_rate == dst_rate && src_channels == dst_channels) {
-        if (src_fmt == dst_fmt) {
+        if (src_format == dst_format) {
             return 0;
         }
 
         /* just a byteswap needed? */
-        if ((src_fmt & ~SDL_AUDIO_MASK_ENDIAN) == (dst_fmt & ~SDL_AUDIO_MASK_ENDIAN)) {
-            if (SDL_AUDIO_BITSIZE(dst_fmt) == 8) {
+        if ((src_format & ~SDL_AUDIO_MASK_ENDIAN) == (dst_format & ~SDL_AUDIO_MASK_ENDIAN)) {
+            if (SDL_AUDIO_BITSIZE(dst_format) == 8) {
                 return 0;
             }
             if (SDL_AddAudioCVTFilter(cvt, SDL_Convert_Byteswap) < 0) {
@@ -760,7 +761,7 @@ int SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
     }
 
     /* Convert data types, if necessary. Updates (cvt). */
-    if (SDL_BuildAudioTypeCVTToFloat(cvt, src_fmt) < 0) {
+    if (SDL_BuildAudioTypeCVTToFloat(cvt, src_format) < 0) {
         return -1; /* shouldn't happen, but just in case... */
     }
 
@

(Patch may be truncated, please check the link at the top of this post.)