Skip to content

Commit 447afeb

Browse files
committed
Fix overflowed()
1 parent adf053f commit 447afeb

File tree

5 files changed

+65
-23
lines changed

5 files changed

+65
-23
lines changed

extras/tests/JsonVariant/set.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,20 @@ TEST_CASE("JsonVariant::set() with not enough memory") {
257257

258258
JsonVariant v = doc.to<JsonVariant>();
259259

260+
SECTION("string literal") {
261+
bool result = v.set("hello world");
262+
263+
REQUIRE(result == false);
264+
REQUIRE(v.isNull());
265+
}
266+
267+
SECTION("static JsonString") {
268+
bool result = v.set(JsonString("hello world", true));
269+
270+
REQUIRE(result == false);
271+
REQUIRE(v.isNull());
272+
}
273+
260274
SECTION("std::string") {
261275
bool result = v.set("hello world!!"_s);
262276

extras/tests/ResourceManager/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
add_executable(ResourceManagerTests
66
allocVariant.cpp
77
clear.cpp
8+
saveStaticString.cpp
89
saveString.cpp
910
shrinkToFit.cpp
1011
size.cpp
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// ArduinoJson - https://arduinojson.org
2+
// Copyright © 2014-2025, Benoit BLANCHON
3+
// MIT License
4+
5+
#include <ArduinoJson/Memory/ResourceManager.hpp>
6+
#include <ArduinoJson/Strings/StringAdapters.hpp>
7+
#include <catch.hpp>
8+
9+
#include "Allocators.hpp"
10+
11+
using namespace ArduinoJson::detail;
12+
13+
TEST_CASE("ResourceManager::saveStaticString() deduplicates strings") {
14+
SpyingAllocator spy;
15+
ResourceManager resources(&spy);
16+
17+
auto str1 = "hello";
18+
auto str2 = "world";
19+
20+
auto id1 = resources.saveStaticString(str1);
21+
auto id2 = resources.saveStaticString(str2);
22+
REQUIRE(id1 != id2);
23+
24+
auto id3 = resources.saveStaticString(str1);
25+
REQUIRE(id1 == id3);
26+
27+
resources.shrinkToFit();
28+
REQUIRE(spy.log() ==
29+
AllocatorLog{
30+
Allocate(sizeofStaticStringPool()),
31+
Reallocate(sizeofStaticStringPool(), sizeofStaticStringPool(2)),
32+
});
33+
REQUIRE(resources.overflowed() == false);
34+
}
35+
36+
TEST_CASE("ResourceManager::saveStaticString() when allocation fails") {
37+
SpyingAllocator spy(FailingAllocator::instance());
38+
ResourceManager resources(&spy);
39+
40+
auto slotId = resources.saveStaticString("hello");
41+
42+
REQUIRE(slotId == NULL_SLOT);
43+
REQUIRE(resources.overflowed() == true);
44+
REQUIRE(spy.log() == AllocatorLog{
45+
AllocateFail(sizeofStaticStringPool()),
46+
});
47+
}

extras/tests/ResourceManager/saveString.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,6 @@ static StringNode* saveString(ResourceManager& resources, const char* s,
1919
return resources.saveString(adaptString(s, n));
2020
}
2121

22-
TEST_CASE("ResourceManager::saveStaticString()") {
23-
SpyingAllocator spy;
24-
ResourceManager resources(&spy);
25-
26-
auto str1 = "hello";
27-
auto str2 = "world";
28-
29-
auto id1 = resources.saveStaticString(str1);
30-
auto id2 = resources.saveStaticString(str2);
31-
REQUIRE(id1 != id2);
32-
33-
auto id3 = resources.saveStaticString(str1);
34-
REQUIRE(id1 == id3);
35-
36-
resources.shrinkToFit();
37-
REQUIRE(spy.log() ==
38-
AllocatorLog{
39-
Allocate(sizeofStaticStringPool()),
40-
Reallocate(sizeofStaticStringPool(), sizeofStaticStringPool(2)),
41-
});
42-
}
43-
4422
TEST_CASE("ResourceManager::saveString()") {
4523
ResourceManager resources;
4624

src/ArduinoJson/Memory/ResourceManager.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ class ResourceManager {
121121
auto slot = staticStringsPools_.allocSlot(allocator_);
122122
if (slot)
123123
*slot = s;
124+
else
125+
overflowed_ = true;
124126

125127
return slot.id();
126128
}
@@ -130,8 +132,8 @@ class ResourceManager {
130132
}
131133

132134
void clear() {
133-
variantPools_.clear(allocator_);
134135
overflowed_ = false;
136+
variantPools_.clear(allocator_);
135137
stringPool_.clear(allocator_);
136138
staticStringsPools_.clear(allocator_);
137139
}

0 commit comments

Comments
 (0)