Commit 69ca7487 authored by Milo Yip's avatar Milo Yip

Manually merge the segfault fix from main branch and fix several unit tests about error code

parent b0059483
......@@ -269,12 +269,16 @@ public:
case '[': ParseArray<parseFlags>(is, handler); break;
default: RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentRootNotObjectOrArray, is.Tell());
}
if (HasParseError())
goto out;
SkipWhitespace(is);
if (is.Peek() != '\0')
RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentRootNotSingular, is.Tell());
}
out:
stack_.Clear();
return !HasParseError();
}
......@@ -440,6 +444,8 @@ private:
if (parseFlags & kParseInsituFlag) {
Ch *head = s.PutBegin();
ParseStringToStream<parseFlags, SourceEncoding, SourceEncoding>(s, s);
if (HasParseError())
return;
size_t length = s.PutEnd(head) - 1;
RAPIDJSON_ASSERT(length <= 0xFFFFFFFF);
handler.String((typename TargetEncoding::Ch*)head, SizeType(length), false);
......@@ -447,6 +453,8 @@ private:
else {
StackStream stackStream(stack_);
ParseStringToStream<parseFlags, SourceEncoding, TargetEncoding>(s, stackStream);
if (HasParseError())
return;
handler.String(stack_.template Pop<typename TargetEncoding::Ch>(stackStream.length_), stackStream.length_ - 1, true);
}
is = s; // Restore is
......
......@@ -335,8 +335,8 @@ TEST(Reader, ParseString_Error) {
TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uABCG\"]");
// The surrogate pair in string is invalid.
TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uD800X\"]");
TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uD800\\uFFFF\"]");
TEST_STRING_ERROR(kParseErrorStringUnicodeSurrogateInvalid, "[\"\\uD800X\"]");
TEST_STRING_ERROR(kParseErrorStringUnicodeSurrogateInvalid, "[\"\\uD800\\uFFFF\"]");
// Missing a closing quotation mark in string.
TEST_STRING_ERROR(kParseErrorStringMissQuotationMark, "[\"Test]");
......@@ -352,7 +352,7 @@ TEST(Reader, ParseString_Error) {
e[2] = c;
ParseErrorCode error = TestString(e);
EXPECT_EQ(kParseErrorStringInvalidEncoding, error);
if (error != kParseErrorNone)
if (error != kParseErrorStringInvalidEncoding)
std::cout << (unsigned)(unsigned char)c << std::endl;
}
}
......@@ -443,8 +443,8 @@ TEST(Reader, ParseArray_Error) {
}
// Missing a comma or ']' after an array element.
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[");
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[}");
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1");
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1}");
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1 2]");
#undef TEST_ARRAY_ERROR
......@@ -590,8 +590,6 @@ TEST(Reader, ParseObject_Error) {
TEST_ERROR(kParseErrorObjectMissColon, "{\"a\",1}");
// Must be a comma or '}' after an object member
TEST_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, "{");
TEST_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, "{]");
TEST_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, "{\"a\":1]");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment