summaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorDaniil Cherednik <[email protected]>2026-06-26 23:32:56 +0200
committerDaniil Cherednik <[email protected]>2026-06-27 10:11:47 +0200
commit75b3d391652e8cb61a98685fcfc5185891007429 (patch)
treeffd320927df61382d18edf650069cf3db2bbe7cd /src/lib
parente9434fe78a4bb68199409e523990d31322e48c7b (diff)
Validate ATRAC1 decoder input and skip malformed framesHEADmaster
The decoder now skips an invalid frame and emits silence.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/bitstream/bitstream.cpp11
1 files changed, 10 insertions, 1 deletions
diff --git a/src/lib/bitstream/bitstream.cpp b/src/lib/bitstream/bitstream.cpp
index ddff907..f731c0a 100644
--- a/src/lib/bitstream/bitstream.cpp
+++ b/src/lib/bitstream/bitstream.cpp
@@ -16,6 +16,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <cstdint>
+#include <stdexcept>
#include "bitstream.h"
#ifndef BIGENDIAN_ORDER
@@ -67,8 +68,16 @@ uint32_t TBitStream::Read(int n) {
const int bytesPos = ReadPos / 8;
const int overlap = ReadPos % 8;
+ // A malformed frame can request more bits than it actually contains.
+ // Reading past the end of the buffer would be out-of-bounds access.
+ if (ReadPos + n > (int)Buf.size() * 8)
+ throw std::runtime_error("read past the end of the bitstream");
+
+ // Number of bytes the requested value actually spans, starting at bytesPos.
+ const int bytesToRead = (overlap + n + 7) / 8;
+
UBytes t;
- for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) {
+ for (int i = 0; i < bytesToRead; ++i) {
#ifdef NBITSTREAM__LITTLE_ENDIAN_CPU
t.bytes[3-i] = (uint8_t)Buf[bytesPos+i];
#else