From 0fd43352be422c72c16ec1d156315012c2c00824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20W=C3=B6rner?= Date: Tue, 23 Apr 2024 22:49:41 +0200 Subject: [PATCH] Added a more informative error message when trying to import WAV files with more than 2 GiB of data. Fixed a possible overflow in AudioStreamWAV::set_data() when trying to set data with a size close to INT_MAX. --- editor/import/resource_importer_wav.cpp | 4 +++- scene/resources/audio_stream_wav.cpp | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/editor/import/resource_importer_wav.cpp b/editor/import/resource_importer_wav.cpp index 6d3d474cee48..2d4feab84818 100644 --- a/editor/import/resource_importer_wav.cpp +++ b/editor/import/resource_importer_wav.cpp @@ -148,7 +148,7 @@ Error ResourceImporterWAV::import(const String &p_source_file, const String &p_s /* chunk size */ uint32_t chunksize = file->get_32(); - uint32_t file_pos = file->get_position(); //save file pos, so we can skip to next chunk safely + uint64_t file_pos = file->get_position(); //save file pos, so we can skip to next chunk safely if (file->eof_reached()) { //ERR_PRINT("EOF REACH"); @@ -197,6 +197,8 @@ Error ResourceImporterWAV::import(const String &p_source_file, const String &p_s break; } + ERR_FAIL_COND_V_MSG(chunksize > INT_MAX, ERR_INVALID_DATA, "WAV file '" + p_source_file + "' is too large (> 2 GiB of data). Try shortening or splitting it or use a compressed format such as Ogg Vorbis or MP3 instead."); + frames = chunksize; if (format_channels == 0) { diff --git a/scene/resources/audio_stream_wav.cpp b/scene/resources/audio_stream_wav.cpp index e2ac0e6d2691..6ee389cbb300 100644 --- a/scene/resources/audio_stream_wav.cpp +++ b/scene/resources/audio_stream_wav.cpp @@ -581,10 +581,10 @@ void AudioStreamWAV::set_data(const Vector &p_data) { data_bytes = 0; } - int datalen = p_data.size(); - if (datalen) { + int64_t datalen = p_data.size(); + if (datalen > 0 && datalen <= INT_MAX) { const uint8_t *r = p_data.ptr(); - int alloc_len = datalen + DATA_PAD * 2; + int64_t alloc_len = datalen + DATA_PAD * 2; data = memalloc(alloc_len); //alloc with some padding for interpolation memset(data, 0, alloc_len); uint8_t *dataptr = (uint8_t *)data;