Improved handling of NULL strings

* Safely assert upon passing NULL string without length
   (requires usage of RAPIDJSON_ASSERT within an expression)
 * Allow using a NULL string together with an explicit length 0
   (GenericStringRef, GenericValue::SetString, ...), see #817
 * Add GenericValue::SetString(StringRefType, Allocator&) overload
 * Add tests for the various cases
This commit is contained in:
Philipp A. Hartmann
2017-07-09 14:46:59 +02:00
parent f1ba61c7ba
commit 47c3c1ec9f
2 changed files with 60 additions and 9 deletions

View File

@@ -857,9 +857,46 @@ TEST(Value, String) {
}
// Issue 226: Value of string type should not point to NULL
TEST(Value, SetStringNullException) {
Value v;
EXPECT_THROW(v.SetString(0, 0), AssertException);
TEST(Value, SetStringNull) {
MemoryPoolAllocator<> allocator;
const char* nullPtr = 0;
{
// Construction with string type creates empty string
Value v(kStringType);
EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
EXPECT_EQ(v.GetStringLength(), 0u);
// Construction from/setting to null without length not allowed
EXPECT_THROW(Value(StringRef(nullPtr)), AssertException);
EXPECT_THROW(Value(StringRef(nullPtr), allocator), AssertException);
EXPECT_THROW(v.SetString(nullPtr, allocator), AssertException);
// Non-empty length with null string is not allowed
EXPECT_THROW(v.SetString(nullPtr, 17u), AssertException);
EXPECT_THROW(v.SetString(nullPtr, 42u, allocator), AssertException);
// Setting to null string with empty length is allowed
v.SetString(nullPtr, 0u);
EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
EXPECT_EQ(v.GetStringLength(), 0u);
v.SetNull();
v.SetString(nullPtr, 0u, allocator);
EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
EXPECT_EQ(v.GetStringLength(), 0u);
}
// Construction with null string and empty length is allowed
{
Value v(nullPtr,0u);
EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
EXPECT_EQ(v.GetStringLength(), 0u);
}
{
Value v(nullPtr, 0u, allocator);
EXPECT_NE(v.GetString(), nullPtr); // non-null string returned
EXPECT_EQ(v.GetStringLength(), 0u);
}
}
template <typename T, typename Allocator>