Godot Engine – Multi-platform 2D and 3D game engine
Fix AudioStreamWAV integer sign sanitizer errors
Aaron Frankefc9d09e629a0
diff --git a/scene/resources/audio/audio_stream_wav.cpp b/scene/resources/audio/audio_stream_wav.cppindex 7517e4b853f..ec5125deba4 100644--- a/scene/resources/audio/audio_stream_wav.cpp+++ b/scene/resources/audio/audio_stream_wav.cpp@@ -1153,12 +1153,13 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_stint ds = data.size();for (int i = 0; i < ds; i++) {+ // Cast to unsigned for storage, but this is really a signed value.if (is16) {int16_t v = CLAMP(data_ptr[i] * 32768, -32768, 32767);- encode_uint16(v, &w[i * 2]);+ encode_uint16((uint16_t)v, &w[i * 2]);} else {int8_t v = CLAMP(data_ptr[i] * 128, -128, 127);- w[i] = v;+ w[i] = (uint8_t)v;}}}diff --git a/tests/scene/test_audio_stream_wav.cpp b/tests/scene/test_audio_stream_wav.cppindex ab3e261f15a..a9628487033 100644--- a/tests/scene/test_audio_stream_wav.cpp+++ b/tests/scene/test_audio_stream_wav.cpp@@ -76,8 +76,9 @@ Vector<uint8_t> gen_pcm8_test(float wav_rate, int wav_count, bool stereo) {// Map sin wave to full range of 8-bit values.uint8_t wav_8bit = Math::fast_ftoi(((wav + 1) / 2) * UINT8_MAX);// Unlike the .wav format, AudioStreamWAV expects signed 8-bit wavs.- uint8_t wav_8bit_signed = wav_8bit - (INT8_MAX + 1);- write_ptr[i] = wav_8bit_signed;+ int8_t wav_8bit_signed = wav_8bit - (INT8_MAX + 1);+ // Cast to uint8_t to match Vector<uint8_t> type, but this is really a signed value.+ write_ptr[i] = (uint8_t)wav_8bit_signed;}return buffer;@@ -104,8 +105,9 @@ Vector<uint8_t> gen_pcm16_test(float wav_rate, int wav_count, bool stereo) {// Map sin wave to full range of 16-bit values.uint16_t wav_16bit = Math::fast_ftoi(((wav + 1) / 2) * UINT16_MAX);// The .wav format expects wavs larger than 8 bits to be signed.- uint16_t wav_16bit_signed = wav_16bit - (INT16_MAX + 1);- encode_uint16(wav_16bit_signed, write_ptr + (i * 2));+ int16_t wav_16bit_signed = wav_16bit - (INT16_MAX + 1);+ // Cast to uint16_t to match encode_uint16 type, but this is really a signed value.+ encode_uint16((uint16_t)wav_16bit_signed, write_ptr + (i * 2));}return buffer;