From 9db10ec4a18d37e404b6f409f89402f6490fa138 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 22 Dec 2023 19:36:42 -0600 Subject: [PATCH] [ox/mc] Fix serialization of negative numbers in non-64 bit values --- deps/ox/src/ox/mc/intops.hpp | 7 ++++--- deps/ox/src/ox/mc/test/tests.cpp | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/deps/ox/src/ox/mc/intops.hpp b/deps/ox/src/ox/mc/intops.hpp index adc4fa9b..52563995 100644 --- a/deps/ox/src/ox/mc/intops.hpp +++ b/deps/ox/src/ox/mc/intops.hpp @@ -64,13 +64,14 @@ struct McInt { template [[nodiscard]] -constexpr McInt encodeInteger(I input) noexcept { +constexpr McInt encodeInteger(I pInput) noexcept { + auto const input = ox::ResizedInt_t{pInput}; McInt out; const auto inputNegative = is_signed_v && input < 0; // move input to uint64_t to allow consistent bit manipulation, and to avoid // overflow concerns uint64_t val = 0; - ox_memcpy(&val, &input, sizeof(I)); + ox_memcpy(&val, &input, sizeof(input)); if (val) { // bits needed to represent number factoring in space possibly // needed for signed bit @@ -93,7 +94,7 @@ constexpr McInt encodeInteger(I input) noexcept { } if (bytes == 9) { out.data[0] = bytesIndicator; - ox_memcpy(&out.data[1], &leVal, sizeof(I)); + ox_memcpy(&out.data[1], &leVal, 8); if (inputNegative) { out.data[1] |= 0b1000'0000; } diff --git a/deps/ox/src/ox/mc/test/tests.cpp b/deps/ox/src/ox/mc/test/tests.cpp index 95b34842..a41dc5c0 100644 --- a/deps/ox/src/ox/mc/test/tests.cpp +++ b/deps/ox/src/ox/mc/test/tests.cpp @@ -267,6 +267,9 @@ std::map tests = { return OxError(0); }; oxAssert(check(uint32_t(14)), "Decode of 14 failed."); + oxAssert(check(int8_t(-1)), "Decode of -1 failed."); + oxAssert(check(int16_t(-1)), "Decode of -1 failed."); + oxAssert(check(int32_t(-1)), "Decode of -1 failed."); oxAssert(check(int64_t(-1)), "Decode of -1 failed."); oxAssert(check(int64_t(-2)), "Decode of -2 failed."); oxAssert(check(int64_t(-127)), "Decode of -127 failed.");