Skip to content

Commit

Permalink
EncodeTlvElement must skip the out-of-memory check when encoding a ze…
Browse files Browse the repository at this point in the history
…ro-length string on ESP32
  • Loading branch information
feasel0 committed Dec 31, 2024
1 parent a9cad82 commit d633cf9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/lib/support/jsontlv/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import("//build_overrides/chip.gni")
import("//build_overrides/jsoncpp.gni")
import("${chip_root}/src/platform/device.gni")

config("jsontlv_config") {
}
Expand Down Expand Up @@ -42,4 +43,9 @@ static_library("jsontlv") {
]

cflags = [ "-Wconversion" ]

if (chip_device_platform == "esp32") {
# malloc(0) is null on this platform.
defines = [ "CONFIG_MALLOC_0_IS_NULL" ]
}
}
8 changes: 7 additions & 1 deletion src/lib/support/jsontlv/JsonToTlv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,13 @@ CHIP_ERROR EncodeTlvElement(const Json::Value & val, TLV::TLVWriter & writer, co

Platform::ScopedMemoryBuffer<uint8_t> byteString;
byteString.Alloc(BASE64_MAX_DECODED_LEN(static_cast<uint16_t>(encodedLen)));
VerifyOrReturnError(byteString.Get() != nullptr, CHIP_ERROR_NO_MEMORY);

// On a platform where malloc(0) is null (which could be misinterpreted as "out of memory")
// we should skip this check if it's a zero-length string.
#ifdef CONFIG_MALLOC_0_IS_NULL
if (encodedLen > 0)
#endif
VerifyOrReturnError(byteString.Get() != nullptr, CHIP_ERROR_NO_MEMORY);

auto decodedLen = Base64Decode(valAsString.c_str(), static_cast<uint16_t>(encodedLen), byteString.Get());
VerifyOrReturnError(decodedLen < UINT16_MAX, CHIP_ERROR_INVALID_ARGUMENT);
Expand Down
10 changes: 2 additions & 8 deletions src/lib/support/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ chip_test_suite("tests") {
"TestFold.cpp",
"TestIniEscaping.cpp",
"TestIntrusiveList.cpp",
"TestJsonToTlv.cpp",
"TestJsonToTlvToJson.cpp",
"TestPersistedCounter.cpp",
"TestPool.cpp",
"TestPrivateHeap.cpp",
Expand All @@ -71,14 +73,6 @@ chip_test_suite("tests") {
if (current_os != "mbed") {
test_sources += [ "TestCHIPArgParser.cpp" ]
}
if (chip_device_platform != "esp32") {
# TODO: Disabled on esp32 due to tests failing.
# https://github.com/project-chip/connectedhomeip/issues/36785
test_sources += [
"TestJsonToTlv.cpp",
"TestJsonToTlvToJson.cpp",
]
}

sources = []

Expand Down

0 comments on commit d633cf9

Please sign in to comment.