diff --git a/README.md b/README.md index ff17f3d4..88fc3984 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![Build Status](https://travis-ci.org/nlohmann/json.svg?branch=master)](https://travis-ci.org/nlohmann/json) [![Build Status](https://ci.appveyor.com/api/projects/status/1acb366xfyg3qybk/branch/develop?svg=true)](https://ci.appveyor.com/project/nlohmann/json) [![Coverage Status](https://img.shields.io/coveralls/nlohmann/json.svg)](https://coveralls.io/r/nlohmann/json) +[![Coverity Scan Build Status](https://scan.coverity.com/projects/5550/badge.svg)](https://scan.coverity.com/projects/nlohmann-json) [![Try online](https://img.shields.io/badge/try-online-blue.svg)](http://melpon.org/wandbox/permlink/fsf5FqYe6GoX68W6) [![Documentation](https://img.shields.io/badge/docs-doxygen-blue.svg)](http://nlohmann.github.io/json) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nlohmann/json/master/LICENSE.MIT) @@ -501,6 +502,7 @@ I deeply appreciate the help of the following people. - [Denis Andrejew](https://github.com/seeekr) fixed a grammar issue in the README file. - [Pierre-Antoine Lacaze](https://github.com/palacaze) found a subtle bug in the `dump()` function. - [TurpentineDistillery](https://github.com/TurpentineDistillery) pointed to [`std::locale::classic()`](http://en.cppreference.com/w/cpp/locale/locale/classic) to avoid too much locale joggling, found some nice performance improvements in the parser and improved the benchmarking code. +- [cgzones](https://github.com/cgzones) had an idea how to fix the Coverity scan. Thanks a lot for helping out! diff --git a/src/json.hpp b/src/json.hpp index 0f6c3c9c..432f9d00 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -7597,6 +7597,12 @@ class basic_json explicit lexer(std::istream& s) : m_stream(&s), m_line_buffer() { + // immediately abort if stream is erroneous + if (s.fail()) + { + throw std::invalid_argument("stream error: " + std::string(strerror(errno))); + } + // fill buffer fill_line_buffer(); diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index c83cd436..8fc3341c 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -7597,6 +7597,12 @@ class basic_json explicit lexer(std::istream& s) : m_stream(&s), m_line_buffer() { + // immediately abort if stream is erroneous + if (s.fail()) + { + throw std::invalid_argument("stream error: " + std::string(strerror(errno))); + } + // fill buffer fill_line_buffer(); diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index 661edc25..50cb190f 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -495,4 +495,25 @@ TEST_CASE("regression tests") json j = json::parse("22e2222"); CHECK(j == json()); } + + SECTION("issue #366 - json::parse on failed stream gets stuck") + { + std::ifstream f("file_not_found.json"); + CHECK_THROWS_AS(json::parse(f), std::invalid_argument); + } + + SECTION("issue #367 - calling stream at EOF") + { + std::stringstream ss; + json j; + ss << "123"; + CHECK_NOTHROW(j << ss); + + // see https://github.com/nlohmann/json/issues/367#issuecomment-262841893: + // ss is not at EOF; this yielded an error before the fix + // (threw basic_string::append). No, it should just throw + // a parse error because of the EOF. + CHECK_THROWS_AS(j << ss, std::invalid_argument); + CHECK_THROWS_WITH(j << ss, "parse error - unexpected end of input"); + } }