From 2d0068d1cb658d6b600fdc3f9ae02fe56338297c Mon Sep 17 00:00:00 2001 From: Vincent Date: Tue, 30 Nov 2021 18:02:42 +0100 Subject: [PATCH] Fix crash when loading an invalid mp3 file --- modules/minimp3/audio_stream_mp3.cpp | 28 +++++++--------------------- modules/minimp3/audio_stream_mp3.h | 2 +- thirdparty/README.md | 13 +++++++++++++ thirdparty/minimp3/minimp3.h | 24 +++++++++++++++++------- thirdparty/minimp3/minimp3_ex.h | 7 +++++-- 5 files changed, 43 insertions(+), 31 deletions(-) diff --git a/modules/minimp3/audio_stream_mp3.cpp b/modules/minimp3/audio_stream_mp3.cpp index 49e9f5f97ec..a9c1f0bb9a7 100644 --- a/modules/minimp3/audio_stream_mp3.cpp +++ b/modules/minimp3/audio_stream_mp3.cpp @@ -125,7 +125,7 @@ AudioStreamPlaybackMP3::~AudioStreamPlaybackMP3() { Ref AudioStreamMP3::instance_playback() { Ref mp3s; - ERR_FAIL_COND_V_MSG(data == nullptr, mp3s, + ERR_FAIL_COND_V_MSG(data.is_empty(), mp3s, "This AudioStreamMP3 does not have an audio file assigned " "to it. AudioStreamMP3 should not be created from the " "inspector or with `.new()`. Instead, load an audio file."); @@ -134,7 +134,7 @@ Ref AudioStreamMP3::instance_playback() { mp3s->mp3_stream = Ref(this); mp3s->mp3d = (mp3dec_ex_t *)memalloc(sizeof(mp3dec_ex_t)); - int errorcode = mp3dec_ex_open_buf(mp3s->mp3d, (const uint8_t *)data, data_len, MP3D_SEEK_TO_SAMPLE); + int errorcode = mp3dec_ex_open_buf(mp3s->mp3d, data.ptr(), data_len, MP3D_SEEK_TO_SAMPLE); mp3s->frames_mixed = 0; mp3s->active = false; @@ -152,11 +152,7 @@ String AudioStreamMP3::get_stream_name() const { } void AudioStreamMP3::clear_data() { - if (data) { - memfree(data); - data = nullptr; - data_len = 0; - } + data.clear(); } void AudioStreamMP3::set_data(const Vector &p_data) { @@ -165,7 +161,7 @@ void AudioStreamMP3::set_data(const Vector &p_data) { mp3dec_ex_t mp3d; int err = mp3dec_ex_open_buf(&mp3d, src_datar, src_data_len, MP3D_SEEK_TO_SAMPLE); - ERR_FAIL_COND(err != 0); + ERR_FAIL_COND_MSG(err || mp3d.info.hz == 0, "Failed to decode mp3 file. Make sure it is a valid mp3 audio file."); channels = mp3d.info.channels; sample_rate = mp3d.info.hz; @@ -175,23 +171,13 @@ void AudioStreamMP3::set_data(const Vector &p_data) { clear_data(); - data = memalloc(src_data_len); - memcpy(data, src_datar, src_data_len); + data.resize(src_data_len); + memcpy(data.ptrw(), src_datar, src_data_len); data_len = src_data_len; } Vector AudioStreamMP3::get_data() const { - Vector vdata; - - if (data_len && data) { - vdata.resize(data_len); - { - uint8_t *w = vdata.ptrw(); - memcpy(w, data, data_len); - } - } - - return vdata; + return data; } void AudioStreamMP3::set_loop(bool p_enable) { diff --git a/modules/minimp3/audio_stream_mp3.h b/modules/minimp3/audio_stream_mp3.h index 3c8bdd8c534..e3adfe683bf 100644 --- a/modules/minimp3/audio_stream_mp3.h +++ b/modules/minimp3/audio_stream_mp3.h @@ -75,7 +75,7 @@ class AudioStreamMP3 : public AudioStream { friend class AudioStreamPlaybackMP3; - void *data = nullptr; + PackedByteArray data; uint32_t data_len = 0; float sample_rate = 1.0; diff --git a/thirdparty/README.md b/thirdparty/README.md index 03e9885bd00..926b5edfafd 100644 --- a/thirdparty/README.md +++ b/thirdparty/README.md @@ -359,6 +359,19 @@ instead of a combination of distance and attribute errors. Patches for both chan found in the `patches` directory. +## minimp3 + +- Upstream: https://github.com/lieff/minimp3 +- Version: git (afb604c06bc8beb145fecd42c0ceb5bda8795144, 2021) +- License: CC0 1.0 + +Files extracted from upstream repository: + +- `minimp3.h` +- `minimp3_ex.h` +- `LICENSE` + + ## miniupnpc - Upstream: https://github.com/miniupnp/miniupnp diff --git a/thirdparty/minimp3/minimp3.h b/thirdparty/minimp3/minimp3.h index 796cbc1f8ed..3220ae1a85b 100644 --- a/thirdparty/minimp3/minimp3.h +++ b/thirdparty/minimp3/minimp3.h @@ -881,12 +881,22 @@ static void L3_midside_stereo(float *left, int n) int i = 0; float *right = left + 576; #if HAVE_SIMD - if (have_simd()) for (; i < n - 3; i += 4) + if (have_simd()) { - f4 vl = VLD(left + i); - f4 vr = VLD(right + i); - VSTORE(left + i, VADD(vl, vr)); - VSTORE(right + i, VSUB(vl, vr)); + for (; i < n - 3; i += 4) + { + f4 vl = VLD(left + i); + f4 vr = VLD(right + i); + VSTORE(left + i, VADD(vl, vr)); + VSTORE(right + i, VSUB(vl, vr)); + } +#ifdef __GNUC__ + /* Workaround for spurious -Waggressive-loop-optimizations warning from gcc. + * For more info see: https://github.com/lieff/minimp3/issues/88 + */ + if (__builtin_constant_p(n % 4 == 0) && n % 4 == 0) + return; +#endif } #endif /* HAVE_SIMD */ for (; i < n; i++) @@ -1353,7 +1363,7 @@ static void mp3d_DCT_II(float *grbuf, int n) } else #endif /* HAVE_SIMD */ #ifdef MINIMP3_ONLY_SIMD - {} + {} /* for HAVE_SIMD=1, MINIMP3_ONLY_SIMD=1 case we do not need non-intrinsic "else" branch */ #else /* MINIMP3_ONLY_SIMD */ for (; k < n; k++) { @@ -1583,7 +1593,7 @@ static void mp3d_synth(float *xl, mp3d_sample_t *dstl, int nch, float *lins) } else #endif /* HAVE_SIMD */ #ifdef MINIMP3_ONLY_SIMD - {} + {} /* for HAVE_SIMD=1, MINIMP3_ONLY_SIMD=1 case we do not need non-intrinsic "else" branch */ #else /* MINIMP3_ONLY_SIMD */ for (i = 14; i >= 0; i--) { diff --git a/thirdparty/minimp3/minimp3_ex.h b/thirdparty/minimp3/minimp3_ex.h index e29dd15b2ec..2871705df3e 100644 --- a/thirdparty/minimp3/minimp3_ex.h +++ b/thirdparty/minimp3/minimp3_ex.h @@ -6,6 +6,7 @@ This software is distributed without any warranty. See . */ +#include #include "minimp3.h" /* flags for mp3dec_ex_open_* functions */ @@ -128,8 +129,10 @@ int mp3dec_ex_open_w(mp3dec_ex_t *dec, const wchar_t *file_name, int flags); #endif #endif /*MINIMP3_EXT_H*/ -#ifdef MINIMP3_IMPLEMENTATION +#if defined(MINIMP3_IMPLEMENTATION) && !defined(_MINIMP3_EX_IMPLEMENTATION_GUARD) +#define _MINIMP3_EX_IMPLEMENTATION_GUARD #include +#include "minimp3.h" static void mp3dec_skip_id3v1(const uint8_t *buf, size_t *pbuf_size) { @@ -1391,4 +1394,4 @@ void mp3dec_ex_close(mp3dec_ex_t *dec) } #endif -#endif /*MINIMP3_IMPLEMENTATION*/ +#endif /* MINIMP3_IMPLEMENTATION && !_MINIMP3_EX_IMPLEMENTATION_GUARD */