#3136: Fixed null character issue when parsing a JSON

This commit is contained in:
Günter Obiltschnig 2021-04-12 20:28:30 +02:00
parent e4b258765e
commit e577e36c25
2 changed files with 15 additions and 1 deletions

View File

@ -204,7 +204,12 @@ void ParserImpl::handle()
break;
}
case JSON_STRING:
if (_pHandler) _pHandler->value(std::string(json_get_string(_pJSON, NULL)));
if (_pHandler)
{
std::size_t length = 0;
const char* val = json_get_string(_pJSON, &length);
_pHandler->value(std::string(val, length == 0 ? 0 : length - 1)); // Decrease the length by 1 because it also contains the terminating null character
}
break;
case JSON_OBJECT:
if (_pHandler) _pHandler->startObject();

View File

@ -1935,6 +1935,15 @@ void JSONTest::testEscape0()
json->stringify(ss);
assertTrue (ss.str().compare("{\"name\":\"B\\u0000b\"}") == 0);
// parse the JSON containing the escaped string
Poco::JSON::Parser parser(new Poco::JSON::ParseHandler());
Var result = parser.parse(ss.str());
assert(result.type() == typeid(Object::Ptr));
Object::Ptr object = result.extract<Object::Ptr>();
assert(object->get("name").extract<std::string>() == nullString);
}