Fixed result of JsonVariant::set((char*)0) (fixes #1368)

This commit is contained in:
Benoit Blanchon
2020-09-05 17:33:47 +02:00
parent 05fc136915
commit d04669d0cc
7 changed files with 92 additions and 92 deletions

View File

@ -347,8 +347,7 @@ class JsonDeserializer {
if (!parseQuotedString())
return false;
const char *value = _stringStorage.save();
variant.setString(make_not_null(value),
typename TStringStorage::storage_policy());
variant.setStringPointer(value, typename TStringStorage::storage_policy());
return true;
}

View File

@ -233,8 +233,7 @@ class MsgPackDeserializer {
const char *s = 0; // <- mute "maybe-uninitialized" (+4 bytes on AVR)
if (!readString(s, n))
return false;
variant.setString(make_not_null(s),
typename TStringStorage::storage_policy());
variant.setStringPointer(s, typename TStringStorage::storage_policy());
return true;
}

View File

@ -1,34 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
#pragma once
#include <ArduinoJson/Namespace.hpp>
#include <ArduinoJson/Polyfills/assert.hpp>
namespace ARDUINOJSON_NAMESPACE {
template <typename T>
class not_null {
public:
explicit not_null(T ptr) : _ptr(ptr) {
ARDUINOJSON_ASSERT(ptr != NULL);
}
T get() const {
ARDUINOJSON_ASSERT(_ptr != NULL);
return _ptr;
}
private:
T _ptr;
};
template <typename T>
not_null<T> make_not_null(T ptr) {
ARDUINOJSON_ASSERT(ptr != NULL);
return not_null<T>(ptr);
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -7,7 +7,6 @@
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Misc/SerializedValue.hpp>
#include <ArduinoJson/Numbers/convertNumber.hpp>
#include <ArduinoJson/Polyfills/gsl/not_null.hpp>
#include <ArduinoJson/Strings/RamStringAdapter.hpp>
#include <ArduinoJson/Variant/VariantContent.hpp>
@ -245,25 +244,14 @@ class VariantData {
setType(VALUE_IS_NULL);
}
void setString(not_null<const char *> s, storage_policies::store_by_copy) {
void setStringPointer(const char *s, storage_policies::store_by_copy) {
setType(VALUE_IS_OWNED_STRING);
_content.asString = s.get();
_content.asString = s;
}
void setString(not_null<const char *> s, storage_policies::store_by_address) {
void setStringPointer(const char *s, storage_policies::store_by_address) {
setType(VALUE_IS_LINKED_STRING);
_content.asString = s.get();
}
template <typename TStoragePolicy>
bool setString(const char *s, TStoragePolicy storage_policy) {
if (s) {
setString(make_not_null(s), storage_policy);
return true;
} else {
setType(VALUE_IS_NULL);
return false;
}
_content.asString = s;
}
template <typename TAdaptedString>
@ -283,14 +271,27 @@ class VariantData {
template <typename TAdaptedString>
inline bool setString(TAdaptedString value, MemoryPool *,
storage_policies::store_by_address) {
return setString(value.data(), storage_policies::store_by_address());
if (value.isNull())
setNull();
else
setStringPointer(value.data(), storage_policies::store_by_address());
return true;
}
template <typename TAdaptedString>
inline bool setString(TAdaptedString value, MemoryPool *pool,
storage_policies::store_by_copy) {
return setString(pool->saveString(value),
storage_policies::store_by_copy());
if (value.isNull()) {
setNull();
return true;
}
const char *copy = pool->saveString(value);
if (!copy) {
setNull();
return false;
}
setStringPointer(copy, storage_policies::store_by_copy());
return true;
}
CollectionData &toArray() {

View File

@ -6,7 +6,6 @@
#include <stdint.h> // int8_t, int16_t
#include <ArduinoJson/Polyfills/gsl/not_null.hpp>
#include <ArduinoJson/Polyfills/type_traits.hpp>
#include <ArduinoJson/Strings/StoragePolicy.hpp>
#include <ArduinoJson/Variant/VariantContent.hpp>