Skip to content

Commit

Permalink
Check for trailing characters when parsing std::string.
Browse files Browse the repository at this point in the history
  • Loading branch information
greggomann committed Oct 20, 2015
1 parent cc130fb commit 7861459
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ test-core: picojson.h test.cc picotest/picotest.c picotest/picotest.h
test-core-int64: picojson.h test.cc picotest/picotest.c picotest/picotest.h
$(CXX) -Wall -DPICOJSON_USE_INT64 test.cc picotest/picotest.c -o $@

parse-test: picojson.h parse-test.cpp
$(CXX) -Wall parse-test.cpp -o $@

clean:
rm -f test-core test-core-int64

Expand Down
7 changes: 6 additions & 1 deletion picojson.h
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,12 @@ namespace picojson {

inline std::string parse(value& out, const std::string& s) {
std::string err;
parse(out, s.begin(), s.end(), &err);
std::string::const_iterator begin = s.begin();
std::string::const_iterator last_char = begin + s.find_last_not_of(" \t\n\r");
std::string::const_iterator end = parse(out, begin, s.end(), &err);
if (end != last_char + 1 && err.empty()) {
err = "Input string contained non-whitespace trailing characters: " + s.substr(end - begin, last_char + 1 - end);
}
return err;
}

Expand Down

0 comments on commit 7861459

Please sign in to comment.