Skip to content

Commit

Permalink
🐛 fixes #714
Browse files Browse the repository at this point in the history
read threw when the input was shorter than the buffer size
  • Loading branch information
nlohmann committed Aug 28, 2017
1 parent 5b71bf0 commit 5439307
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/json.hpp
Expand Up @@ -1413,10 +1413,8 @@ class cached_input_stream_adapter : public input_adapter_protocol
private:
void fill_buffer()
{
// fill
is.read(buffer.data(), static_cast<std::streamsize>(buffer.size()));
// store number of bytes in the buffer
fill_size = static_cast<size_t>(is.gcount());
// fill buffer
fill_size = static_cast<std::size_t>(is.rdbuf()->sgetn(buffer.data(), static_cast<std::streamsize>(buffer.size())));
}

/// the associated input stream
Expand Down
35 changes: 31 additions & 4 deletions test/src/unit-regression.cpp
Expand Up @@ -1209,9 +1209,9 @@ TEST_CASE("regression tests")
{
SECTION("original example")
{
std::valarray<double> v;
nlohmann::json j;
j["test"] = v;
std::valarray<double> v;
nlohmann::json j;
j["test"] = v;
}

SECTION("full example")
Expand All @@ -1230,7 +1230,34 @@ TEST_CASE("regression tests")

CHECK_THROWS_AS(json().get<std::valarray<double>>(), json::type_error&);
CHECK_THROWS_WITH(json().get<std::valarray<double>>(),
"[json.exception.type_error.302] type must be array, but is null");
"[json.exception.type_error.302] type must be array, but is null");
}
}

SECTION("issue #714 - throw std::ios_base::failure exception when failbit set to true")
{
{
std::ifstream is;
is.exceptions(
is.exceptions()
| std::ios_base::failbit
| std::ios_base::badbit
); // handle different exceptions as 'file not found', 'permission denied'

is.open("test/data/regression/working_file.json");
CHECK_NOTHROW(nlohmann::json::parse(is));
}

{
std::ifstream is;
is.exceptions(
is.exceptions()
| std::ios_base::failbit
| std::ios_base::badbit
); // handle different exceptions as 'file not found', 'permission denied'

is.open("test/data/json_nlohmann_tests/all_unicode.json.cbor");
CHECK_NOTHROW(nlohmann::json::from_cbor(is));
}
}
}

0 comments on commit 5439307

Please sign in to comment.