diff --git a/src/JsonParser.cc b/src/JsonParser.cc index 5f83ec7a..bae75286 100644 --- a/src/JsonParser.cc +++ b/src/JsonParser.cc @@ -107,7 +107,7 @@ ssize_t JsonParser::parseUpdate(const char* data, size_t size) } else if(currentState_ == JSON_ERROR) { return lastError_; } - for(i = 0; i < size; ++i) { + for(i = 0; i < size && currentState_ != JSON_FINISH; ++i) { char c = data[i]; switch(currentState_) { case JSON_ARRAY: @@ -499,9 +499,6 @@ ssize_t JsonParser::parseUpdate(const char* data, size_t size) break; } } - if(currentState_ == JSON_FINISH) { - break; - } } return i; } diff --git a/test/ValueBaseJsonParserTest.cc b/test/ValueBaseJsonParserTest.cc index 97443d2b..546206d8 100644 --- a/test/ValueBaseJsonParserTest.cc +++ b/test/ValueBaseJsonParserTest.cc @@ -220,7 +220,15 @@ void ValueBaseJsonParserTest::testParseUpdate() const String* s = downcast(list->get(0)); CPPUNIT_ASSERT_EQUAL(std::string("foo$b¢€baz"), s->s()); } - + { + // ignore garbage at the end of the input. + std::string src = "[]trail"; + SharedHandle r = parser.parseFinal(src.c_str(), src.size(), + error); + const List* list = downcast(r); + CPPUNIT_ASSERT(list); + CPPUNIT_ASSERT_EQUAL((ssize_t)2, error); + } } namespace {