Skip to content

Commit

Permalink
MsgPackDeserializer: check extension allocation result
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Sep 4, 2024
1 parent 1f7a3f3 commit 3b64197
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
41 changes: 41 additions & 0 deletions extras/tests/MsgPackDeserializer/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
16 changes: 10 additions & 6 deletions src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,16 @@ class MsgPackDeserializer {

if (isSigned) {
auto truncatedValue = static_cast<JsonInteger>(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<JsonUInt>(unsignedValue);
if (truncatedValue == unsignedValue)
variant->setInteger(truncatedValue, resources_);
if (!variant->setInteger(truncatedValue, resources_))
return DeserializationError::NoMemory;
// else set null on overflow
}

Expand Down Expand Up @@ -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 <typename T>
Expand Down

0 comments on commit 3b64197

Please sign in to comment.