From 9620bb0a6d399ba04f89f575f98eb53b3b252020 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 24 Jul 2012 23:47:18 +0900 Subject: [PATCH] Fix off by 1 error in JsonParser::parseUpdate() return value --- src/JsonParser.cc | 5 +---- test/ValueBaseJsonParserTest.cc | 10 +++++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/JsonParser.cc b/src/JsonParser.cc index 5f83ec7a9..bae752866 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 97443d2b2..546206d8f 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 {