diff --git a/src/soundloader_opus.cpp b/src/soundloader_opus.cpp index 79252641cf..9e1860f3be 100644 --- a/src/soundloader_opus.cpp +++ b/src/soundloader_opus.cpp @@ -9,6 +9,7 @@ #include "stdafx.h" +#include "debug.h" #include "misc/autorelease.hpp" #include "random_access_file_type.h" #include "sound_type.h" @@ -54,7 +55,10 @@ public: int error = 0; auto of = AutoRelease(op_open_memory(tmp.data(), tmp.size(), &error)); - if (error != 0) return false; + if (error != 0) { + Debug(grf, 0, "SoundLoader_Opus: Unable to open stream."); + return false; + } size_t datapos = 0; for (;;) { @@ -64,8 +68,15 @@ public: int read = op_read(of.get(), reinterpret_cast(&data[datapos]), DECODE_BUFFER_BYTES, &link_index); if (read == 0) break; - if (read < 0 || op_channel_count(of.get(), link_index) != 1) { - /* Error reading, or incorrect channel count. */ + if (read < 0) { + Debug(grf, 0, "SoundLoader_Opus: Unexpected end of stream."); + data.clear(); + return false; + } + + int channels = op_channel_count(of.get(), link_index); + if (channels != 1) { + Debug(grf, 0, "SoundLoader_Opus: Unsupported channels {}, expected 1.", channels); data.clear(); return false; } diff --git a/src/soundloader_wav.cpp b/src/soundloader_wav.cpp index 72dbf0a056..77db237034 100644 --- a/src/soundloader_wav.cpp +++ b/src/soundloader_wav.cpp @@ -8,8 +8,8 @@ /** @file soundloader_wav.cpp Loading of wav sounds. */ #include "stdafx.h" -#include "core/bitmath_func.hpp" #include "core/math_func.hpp" +#include "debug.h" #include "random_access_file_type.h" #include "sound_type.h" #include "soundloader_type.h" @@ -39,10 +39,16 @@ public: if (tag == std::byteswap('fmt ')) { uint16_t format = file.ReadWord(); - if (format != 1) return false; // File must be uncompressed PCM + if (format != 1) { + Debug(grf, 0, "SoundLoader_Wav: Unsupported format {}, expected 1 (uncompressed PCM).", format); + return false; + } sound.channels = file.ReadWord(); - if (sound.channels != 1) return false; // File must be mono. + if (sound.channels != 1) { + Debug(grf, 0, "SoundLoader_Wav: Unsupported channels {}, expected 1.", sound.channels); + return false; + } sound.rate = file.ReadDword(); if (!new_format) sound.rate = DEFAULT_SAMPLE_RATE; // All old samples should be played at 11025 Hz. @@ -51,13 +57,20 @@ public: file.ReadWord(); // alignment sound.bits_per_sample = file.ReadWord(); - if (sound.bits_per_sample != 8 && sound.bits_per_sample != 16) return false; // File must be 8 or 16 BPS. + if (sound.bits_per_sample != 8 && sound.bits_per_sample != 16) { + Debug(grf, 0, "SoundLoader_Wav: Unsupported bits_per_sample {}, expected 8 or 16.", sound.bits_per_sample); + return false; + } /* We've read 16 bytes of this chunk, we can skip anything extra. */ size -= 16; } else if (tag == std::byteswap('data')) { uint align = sound.channels * sound.bits_per_sample / 8; - if (Align(size, align) != size) return false; // Ensure length is aligned correctly for channels and BPS. + if (Align(size, align) != size) { + /* Ensure length is aligned correctly for channels and BPS. */ + Debug(grf, 0, "SoundLoader_Wav: Unexpected end of stream."); + return false; + } if (size == 0) return true; // No need to continue.