From 3b641978695c611a621e4dc15efd97b003ed731b Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Wed, 4 Sep 2024 14:34:02 +0200 Subject: [PATCH] MsgPackDeserializer: check extension allocation result --- extras/tests/MsgPackDeserializer/errors.cpp | 41 +++++++++++++++++++ .../MsgPack/MsgPackDeserializer.hpp | 16 +++++--- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/extras/tests/MsgPackDeserializer/errors.cpp b/extras/tests/MsgPackDeserializer/errors.cpp index 01e761af2..dff712ab4 100644 --- a/extras/tests/MsgPackDeserializer/errors.cpp +++ b/extras/tests/MsgPackDeserializer/errors.cpp @@ -199,3 +199,44 @@ TEST_CASE( REQUIRE(err == DeserializationError::NoMemory); } } + +TEST_CASE( + "deserializeMsgPack() returns NoMemory if extension allocation fails") { + JsonDocument doc(FailingAllocator::instance()); + + SECTION("uint32_t should pass") { + auto err = deserializeMsgPack(doc, "\xceXXXX"); + + REQUIRE(err == DeserializationError::Ok); + } + + SECTION("uint64_t should fail") { + auto err = deserializeMsgPack(doc, "\xcfXXXXXXXX"); + + REQUIRE(err == DeserializationError::NoMemory); + } + + SECTION("int32_t should pass") { + auto err = deserializeMsgPack(doc, "\xd2XXXX"); + + REQUIRE(err == DeserializationError::Ok); + } + + SECTION("int64_t should fail") { + auto err = deserializeMsgPack(doc, "\xd3XXXXXXXX"); + + REQUIRE(err == DeserializationError::NoMemory); + } + + SECTION("float should pass") { + auto err = deserializeMsgPack(doc, "\xcaXXXX"); + + REQUIRE(err == DeserializationError::Ok); + } + + SECTION("double should fail") { + auto err = deserializeMsgPack(doc, "\xcbXXXXXXXX"); + + REQUIRE(err == DeserializationError::NoMemory); + } +} diff --git a/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp b/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp index 7b64299a1..0c32f58b9 100644 --- a/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp +++ b/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp @@ -230,13 +230,16 @@ class MsgPackDeserializer { if (isSigned) { auto truncatedValue = static_cast(signedValue); - if (truncatedValue == signedValue) - variant->setInteger(truncatedValue, resources_); + if (truncatedValue == signedValue) { + if (!variant->setInteger(truncatedValue, resources_)) + return DeserializationError::NoMemory; + } // else set null on overflow } else { auto truncatedValue = static_cast(unsignedValue); if (truncatedValue == unsignedValue) - variant->setInteger(truncatedValue, resources_); + if (!variant->setInteger(truncatedValue, resources_)) + return DeserializationError::NoMemory; // else set null on overflow } @@ -270,9 +273,10 @@ class MsgPackDeserializer { return err; fixEndianness(value); - variant->setFloat(value, resources_); - - return DeserializationError::Ok; + if (variant->setFloat(value, resources_)) + return DeserializationError::Ok; + else + return DeserializationError::NoMemory; } template