diff --git a/src/Internals/JsonParser.cpp b/src/Internals/JsonParser.cpp index 70c5f98d..de736c02 100644 --- a/src/Internals/JsonParser.cpp +++ b/src/Internals/JsonParser.cpp @@ -74,9 +74,9 @@ void JsonParser::parseAnythingTo(JsonValue &destination) { } JsonArray &JsonParser::parseArray() { - skip('['); + if (!skip('[')) return JsonArray::invalid(); // missing opening bracket - if (isEnd()) return JsonArray::invalid(); + if (isEnd()) return JsonArray::invalid(); // end of stream JsonArray &array = _buffer->createArray(); if (skip(']')) return array; // empty array @@ -127,7 +127,7 @@ void JsonParser::parseNullTo(JsonValue &destination) { } JsonObject &JsonParser::parseObject() { - skip('{'); + if (!skip('{')) return JsonObject::invalid(); // missing opening brace if (isEnd()) return JsonObject::invalid(); // premature ending diff --git a/test/JsonParser_Array_Tests.cpp b/test/JsonParser_Array_Tests.cpp index db3ef8a2..c4ba4922 100644 --- a/test/JsonParser_Array_Tests.cpp +++ b/test/JsonParser_Array_Tests.cpp @@ -58,9 +58,13 @@ TEST_F(JsonParser_Array_Tests, EmptyArray) { sizeMustBe(0); } +TEST_F(JsonParser_Array_Tests, MissingOpeningBracket) { + whenInputIs("]"); + parseMustFail(); +} + TEST_F(JsonParser_Array_Tests, ArrayWithNoEnd) { whenInputIs("["); - parseMustFail(); } diff --git a/test/JsonParser_Object_Tests.cpp b/test/JsonParser_Object_Tests.cpp index 95403610..5d25c536 100644 --- a/test/JsonParser_Object_Tests.cpp +++ b/test/JsonParser_Object_Tests.cpp @@ -45,22 +45,24 @@ TEST_F(JsonParser_Object_Test, EmptyObject) { sizeMustBe(0); } +TEST_F(JsonParser_Object_Test, MissingOpeningBrace) { + whenInputIs("}"); + parseMustFail(); +} + TEST_F(JsonParser_Object_Test, MissingClosingBrace) { whenInputIs("{"); parseMustFail(); - sizeMustBe(0); } TEST_F(JsonParser_Object_Test, MissingColonAndValue) { whenInputIs("{\"key\"}"); parseMustFail(); - sizeMustBe(0); } TEST_F(JsonParser_Object_Test, MissingQuotesAndColonAndValue) { whenInputIs("{key}"); parseMustFail(); - sizeMustBe(0); } TEST_F(JsonParser_Object_Test, OneString) {