From 83fb743c6bf94a6af88977a68648d6857bac9840 Mon Sep 17 00:00:00 2001
From: Wohlstand <[EMAIL REDACTED]>
Date: Tue, 31 May 2022 09:30:26 +0300
Subject: [PATCH] load_voc.c: Fixed Clang warning
Clang Code Model noticed the possible recognition of `v.size` as an uninitialized data which may lead to unexpected behavior. Also, make sure `SDL_malloc()` will never be called with a zero value of `v.rest`, and avoid leading division by zero (when `*audio_len` is divided by `v.size`)
(cherry picked from commit 4be422bf8f4b5f531906000db905c1e6f0835b7d)
---
load_voc.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/load_voc.c b/load_voc.c
index f12d4d6e..3ebde36d 100644
--- a/load_voc.c
+++ b/load_voc.c
@@ -392,6 +392,7 @@ SDL_AudioSpec *Mix_LoadVOC_RW (SDL_RWops *src, int freesrc,
if ( !voc_check_header(src) )
goto done;
+ memset(&v, 0, sizeof (vs_t));
v.rate = VOC_BAD_RATE;
v.rest = 0;
v.has_extended = 0;
@@ -407,12 +408,17 @@ SDL_AudioSpec *Mix_LoadVOC_RW (SDL_RWops *src, int freesrc,
goto done;
}
+ if (v.size == 0) {
+ SDL_SetError("VOC data had invalid word size!");
+ goto done;
+ }
+
spec->format = ((v.size == ST_SIZE_WORD) ? AUDIO_S16 : AUDIO_U8);
if (spec->channels == 0)
spec->channels = v.channels;
*audio_len = v.rest;
- *audio_buf = SDL_malloc(v.rest);
+ *audio_buf = (v.rest == 0) ? NULL : SDL_malloc(v.rest);
if (*audio_buf == NULL)
goto done;