Skip to content

Commit 3b64197

Browse files
committed
MsgPackDeserializer: check extension allocation result
1 parent 1f7a3f3 commit 3b64197

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

extras/tests/MsgPackDeserializer/errors.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,44 @@ TEST_CASE(
199199
REQUIRE(err == DeserializationError::NoMemory);
200200
}
201201
}
202+
203+
TEST_CASE(
204+
"deserializeMsgPack() returns NoMemory if extension allocation fails") {
205+
JsonDocument doc(FailingAllocator::instance());
206+
207+
SECTION("uint32_t should pass") {
208+
auto err = deserializeMsgPack(doc, "\xceXXXX");
209+
210+
REQUIRE(err == DeserializationError::Ok);
211+
}
212+
213+
SECTION("uint64_t should fail") {
214+
auto err = deserializeMsgPack(doc, "\xcfXXXXXXXX");
215+
216+
REQUIRE(err == DeserializationError::NoMemory);
217+
}
218+
219+
SECTION("int32_t should pass") {
220+
auto err = deserializeMsgPack(doc, "\xd2XXXX");
221+
222+
REQUIRE(err == DeserializationError::Ok);
223+
}
224+
225+
SECTION("int64_t should fail") {
226+
auto err = deserializeMsgPack(doc, "\xd3XXXXXXXX");
227+
228+
REQUIRE(err == DeserializationError::NoMemory);
229+
}
230+
231+
SECTION("float should pass") {
232+
auto err = deserializeMsgPack(doc, "\xcaXXXX");
233+
234+
REQUIRE(err == DeserializationError::Ok);
235+
}
236+
237+
SECTION("double should fail") {
238+
auto err = deserializeMsgPack(doc, "\xcbXXXXXXXX");
239+
240+
REQUIRE(err == DeserializationError::NoMemory);
241+
}
242+
}

src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,16 @@ class MsgPackDeserializer {
230230

231231
if (isSigned) {
232232
auto truncatedValue = static_cast<JsonInteger>(signedValue);
233-
if (truncatedValue == signedValue)
234-
variant->setInteger(truncatedValue, resources_);
233+
if (truncatedValue == signedValue) {
234+
if (!variant->setInteger(truncatedValue, resources_))
235+
return DeserializationError::NoMemory;
236+
}
235237
// else set null on overflow
236238
} else {
237239
auto truncatedValue = static_cast<JsonUInt>(unsignedValue);
238240
if (truncatedValue == unsignedValue)
239-
variant->setInteger(truncatedValue, resources_);
241+
if (!variant->setInteger(truncatedValue, resources_))
242+
return DeserializationError::NoMemory;
240243
// else set null on overflow
241244
}
242245

@@ -270,9 +273,10 @@ class MsgPackDeserializer {
270273
return err;
271274

272275
fixEndianness(value);
273-
variant->setFloat(value, resources_);
274-
275-
return DeserializationError::Ok;
276+
if (variant->setFloat(value, resources_))
277+
return DeserializationError::Ok;
278+
else
279+
return DeserializationError::NoMemory;
276280
}
277281

278282
template <typename T>

0 commit comments

Comments
 (0)