mirror of
https://github.com/Tencent/rapidjson.git
synced 2025-10-28 11:31:57 +01:00
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:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user