open-source-parsers / open-source-parsers/jsoncpp

OurReader::DecodeNumber leads to bad value type

Open Beginner friendly
#1,341 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
8.9k
Forks
2.7k
Avg merge
31m
Merged PRs (30d)
1

Description

We are using Jsoncpp 1.9.4 with Viual Studio 2015 in a Win32 configuration.
We just have changed from Json::Reader to Json::CharReaderBuilder.
When parsing the following text { "test":-1} to Json::Value val
the expresstion val["test"].asInt() leads to a

JSON_ASSERT_MESSAGE(..., "LargestUInt out of Int range"),

as the type computed in OurReader::decodeNumber(Token& token, Value& decoded) is UInt.

Expected behavior
I think a parsed value of -1 should lead to a Json::Value of type int.
When I changed the followig code from

OurReader::decodeNumber(Token& token, Value& decoded)
....
if (isNegative)
{
	// We use the same magnitude assumption here, just in case.
	const auto last_digit = static_cast<Value::Int>(value % 10);// changed from Value:Uint to Value::Int
	decoded = -Value::LargestInt(value / 10) * 10 - last_digit;
}

...
the problem is solved.

Desktop (please complete the following information):

  • OS: Windows 10

Additional context
Visual Studio 2015 Win32 as target version

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Search for OurReader::decodeNumber(Token& token, Value& decoded) and reproduce parsing {"test":-1} with Json::CharReaderBuilder, then check val["test"].asInt(). Add a regression test covering the negative value and confirm the parsed value can be read as an int without the range assertion.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.