Check for NUL terminator in MemoryPool::findString()

This commit is contained in:
Benoit Blanchon
2021-11-20 20:30:24 +01:00
parent 43b2e2e774
commit a27398e445
9 changed files with 64 additions and 3 deletions

View File

@@ -166,9 +166,21 @@ class MemoryPool {
#if ARDUINOJSON_ENABLE_STRING_DEDUPLICATION
template <typename TAdaptedString>
const char* findString(const TAdaptedString& str) {
for (char* next = _begin; next < _left; ++next) {
if (str.compare(next) == 0)
static bool stringEquals(const char* p, size_t n, const TAdaptedString& s) {
if (p[n]) // check terminator first
return false;
for (size_t i = 0; i < n; i++) {
if (p[i] != s[i])
return false;
}
return true;
}
template <typename TAdaptedString>
const char* findString(const TAdaptedString& str) const {
size_t n = str.size();
for (char* next = _begin; next + n < _left; ++next) {
if (stringEquals(next, n, str))
return next;
// jump to next terminator