Skip to content

Defect in Var::parseString when there is no space between value and newline; includes fix #362

@penntaylor

Description

@penntaylor

Current Poco 1.5.2-all contains a defect in Var::parseString. When the value being processed does not begin with double quotes ("), parseString treats a newline control character (\n) as part of the value rather than as a terminator. Since the JSON spec does not allow a newline control character as part of a value unless the value is a string, this is incorrect behavior.

In the example code below, the difference between the string stored in 'works' and the one stored in 'fails' is that 'works' contains a space between the 2 and the newline, while 'fails' does not. Both are valid JSON.

std::string works("{ \"a\" : 1, \"b\" : 2 \n}");
Poco::Dynamic::Var v = Poco::Dynamic::Var::parse( works );
std::cout << v.toString() << std::endl;

std::string fails("{ \"a\" : 1, \"b\" : 2\n}");
v = Poco::Dynamic::Var::parse( fails );
std::cout << v.toString() << std::endl;

Current Poco 1.5.2-all, produces this output:

{ "a" : "1", "b" : "2" }
terminate called after throwing an instance of 'Poco::DataFormatException'
  what():  Bad data format

SOLUTION: Changing line 488 of Var.cpp from

static const std::string OTHER_STOP(" ,]}"); // we stop at space, ',', ']' or '}'

to

static const std::string OTHER_STOP("\n ,]}"); // we stop at newline, space, ',', ']' or '}'

fixes the issue, and produces the expected output:

{ "a" : "1", "b" : "2" }
{ "a" : "1", "b" : "2" }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions