Skip to content

Commit

Permalink
✅ tests and fix for #367
Browse files Browse the repository at this point in the history
operator>> now works as expected.
  • Loading branch information
nlohmann committed May 21, 2017
1 parent 16b63d3 commit 9e507df
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8826,8 +8826,10 @@ class basic_json
{
// clear stream flags
is.clear();
// set stream after last processed char
is.seekg(start_position + static_cast<std::streamoff>(processed_chars - 1));
// We initially read a lot of characters into the buffer, and we
// may not have processed all of them. Therefore, we need to
// "rewind" the stream after the last processed char.
is.seekg(start_position + static_cast<std::streamoff>(processed_chars));
}

int get_character() override
Expand All @@ -8840,20 +8842,19 @@ class basic_json
// store number of bytes in the buffer
fill_size = static_cast<size_t>(is.gcount());

// the buffer is ready
buffer_pos = 0;

// remember that filling did not yield new input
if (fill_size == 0)
{
eof = true;
return std::char_traits<char>::eof();
}

// the buffer is ready
buffer_pos = 0;
}

++processed_chars;
return eof
? std::char_traits<char>::eof()
: buffer[buffer_pos++] & 0xFF;
return buffer[buffer_pos++] & 0xFF;;
}

std::string read(size_t offset, size_t length) override
Expand Down
112 changes: 112 additions & 0 deletions test/src/unit-regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,118 @@ TEST_CASE("regression tests")
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}

SECTION("issue #367 - behavior of operator>> should more closely resemble that of built-in overloads")
{
SECTION("(empty)")
{
std::stringstream ss;
json j;
CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}

SECTION("(whitespace)")
{
std::stringstream ss;
ss << " ";
json j;
CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}

SECTION("one value")
{
std::stringstream ss;
ss << "111";
json j;
CHECK_NOTHROW(ss >> j);
CHECK(j == 111);

CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}

SECTION("one value + whitespace")
{
std::stringstream ss;
ss << "222 \t\n";
json j;
CHECK_NOTHROW(ss >> j);
CHECK(j == 222);

CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}

SECTION("whitespace + one value")
{
std::stringstream ss;
ss << "\n\t 333";
json j;
CHECK_NOTHROW(ss >> j);
CHECK(j == 333);

CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}

SECTION("three values")
{
std::stringstream ss;
ss << " 111 \n222\n \n 333";
json j;
CHECK_NOTHROW(ss >> j);
CHECK(j == 111);
CHECK_NOTHROW(ss >> j);
CHECK(j == 222);
CHECK_NOTHROW(ss >> j);
CHECK(j == 333);

CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}

SECTION("literals without whitespace")
{
std::stringstream ss;
ss << "truefalsenull\"\"";
json j;
CHECK_NOTHROW(ss >> j);
CHECK(j == true);
CHECK_NOTHROW(ss >> j);
CHECK(j == false);
CHECK_NOTHROW(ss >> j);
CHECK(j == nullptr);
CHECK_NOTHROW(ss >> j);
CHECK(j == "");

CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}

SECTION("example from #529")
{
std::stringstream ss;
ss << "{\n \"one\" : 1,\n \"two\" : 2\n}\n{\n \"three\" : 3\n}";
json j;
CHECK_NOTHROW(ss >> j);
CHECK(j == json({{"one", 1}, {"two", 2}}));
CHECK_NOTHROW(ss >> j);
CHECK(j == json({{"three", 3}}));

CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}
}

SECTION("issue #389 - Integer-overflow (OSS-Fuzz issue 267)")
{
// original test case
Expand Down

0 comments on commit 9e507df

Please sign in to comment.