anhero / JsonBox

This is a JSON C++ library. It can write and read JSON files with ease and speed.
MIT License
115 stars 60 forks source link

Infinite loop in Value::readObject(...) stream checking #40

Open PerGraa opened 8 years ago

PerGraa commented 8 years ago

Playing around with the afl fuzzer, I have encountered a hang in the (time of writing) trunk code.

Test case: JsonBox_hang00.json.txt

Sure the input is not valid JSON, but it triggers something interesting.

Using Value::loadFromString(...) which uses Value::loadFromStream(...) which in the Structural::BEGIN_OBJECT case calls Value::readObject(...) we end up in the innermost while loop of readObject.

That loop runs infinite since input.eof() stays low, but in debugging I can see that input.fail() goes high, properly due to the fuzzed bad input file.

Referring to the truth table at http://en.cppreference.com/w/cpp/io/basic_ios/eof a possible fix could be to use input.good() instead of !input.eof(), but I'll leave that up to the developers.

Note that !input.eof() is used a number of places in Value.cpp.