From 817e3fcef127e786a12d09385ea7e4e90c5a420c Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 17 Mar 2019 01:11:12 -0500 Subject: [PATCH] [ox/mc] Add bytes read to decodeInteger and fix overreading --- deps/ox/src/ox/mc/intops.hpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/deps/ox/src/ox/mc/intops.hpp b/deps/ox/src/ox/mc/intops.hpp index 1762e8d3..b6e6ac6a 100644 --- a/deps/ox/src/ox/mc/intops.hpp +++ b/deps/ox/src/ox/mc/intops.hpp @@ -10,6 +10,7 @@ #include #include +#include namespace ox::mc { @@ -117,12 +118,15 @@ static_assert(countBytes(0b01111111) == 8); static_assert(countBytes(0b11111111) == 9); template -[[nodiscard]] ValErr decodeInteger(uint8_t buff[9], std::size_t buffLen) noexcept { +[[nodiscard]] ValErr decodeInteger(uint8_t buff[9], std::size_t buffLen, std::size_t *bytesRead) noexcept { const auto bytes = countBytes(buff[0]); if (bytes == 9) { + *bytesRead = bytes; return {LittleEndian(*reinterpret_cast(&buff[1])), 0}; } else if (buffLen >= bytes) { - uint64_t decoded = LittleEndian(*reinterpret_cast(&buff[0])); + *bytesRead = bytes; + uint64_t decoded = 0; + ox_memcpy(&decoded, &buff[0], bytes); decoded >>= bytes; auto out = static_cast(decoded); // move sign bit @@ -142,7 +146,8 @@ template template [[nodiscard]] ValErr decodeInteger(McInt m) noexcept { - return decodeInteger(m.data, 9); + std::size_t bytesRead; + return decodeInteger(m.data, 9, &bytesRead); } }