🚑 fix for #493
Added a test to check if the input stream is good() before executing getline on it. Also added two test cases that set the failbit and badbit before calling file_line_buffer.
This commit is contained in:
parent
ff0b18d10c
commit
4e49829851
3 changed files with 46 additions and 0 deletions
|
@ -10988,6 +10988,13 @@ basic_json_parser_74:
|
||||||
m_line_buffer.erase(0, num_processed_chars);
|
m_line_buffer.erase(0, num_processed_chars);
|
||||||
// read next line from input stream
|
// read next line from input stream
|
||||||
m_line_buffer_tmp.clear();
|
m_line_buffer_tmp.clear();
|
||||||
|
|
||||||
|
// check if stream is still good
|
||||||
|
if (not m_stream->good())
|
||||||
|
{
|
||||||
|
JSON_THROW(std::invalid_argument("stream error"));
|
||||||
|
}
|
||||||
|
|
||||||
std::getline(*m_stream, m_line_buffer_tmp, '\n');
|
std::getline(*m_stream, m_line_buffer_tmp, '\n');
|
||||||
|
|
||||||
// add line with newline symbol to the line buffer
|
// add line with newline symbol to the line buffer
|
||||||
|
|
|
@ -10022,6 +10022,13 @@ class basic_json
|
||||||
m_line_buffer.erase(0, num_processed_chars);
|
m_line_buffer.erase(0, num_processed_chars);
|
||||||
// read next line from input stream
|
// read next line from input stream
|
||||||
m_line_buffer_tmp.clear();
|
m_line_buffer_tmp.clear();
|
||||||
|
|
||||||
|
// check if stream is still good
|
||||||
|
if (not m_stream->good())
|
||||||
|
{
|
||||||
|
JSON_THROW(std::invalid_argument("stream error"));
|
||||||
|
}
|
||||||
|
|
||||||
std::getline(*m_stream, m_line_buffer_tmp, '\n');
|
std::getline(*m_stream, m_line_buffer_tmp, '\n');
|
||||||
|
|
||||||
// add line with newline symbol to the line buffer
|
// add line with newline symbol to the line buffer
|
||||||
|
|
|
@ -28,6 +28,7 @@ SOFTWARE.
|
||||||
|
|
||||||
#include "catch.hpp"
|
#include "catch.hpp"
|
||||||
|
|
||||||
|
#define private public
|
||||||
#include "json.hpp"
|
#include "json.hpp"
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
|
@ -804,4 +805,35 @@ TEST_CASE("regression tests")
|
||||||
|
|
||||||
CHECK(j["bool_vector"].dump() == "[false,true,false,false]");
|
CHECK(j["bool_vector"].dump() == "[false,true,false,false]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("issue #495 - fill_line_buffer incorrectly tests m_stream for eof but not fail or bad bits")
|
||||||
|
{
|
||||||
|
SECTION("setting failbit")
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "[1,2,3\n,4,5]";
|
||||||
|
|
||||||
|
json::lexer l(ss);
|
||||||
|
|
||||||
|
CHECK(l.m_line_buffer == "[1,2,3\n");
|
||||||
|
|
||||||
|
l.m_stream->setstate(std::ios_base::failbit);
|
||||||
|
|
||||||
|
CHECK_THROWS_AS(l.fill_line_buffer(), std::invalid_argument);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("setting badbit")
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "[1,2,3\n,4,5]";
|
||||||
|
|
||||||
|
json::lexer l(ss);
|
||||||
|
|
||||||
|
CHECK(l.m_line_buffer == "[1,2,3\n");
|
||||||
|
|
||||||
|
l.m_stream->setstate(std::ios_base::badbit);
|
||||||
|
|
||||||
|
CHECK_THROWS_AS(l.fill_line_buffer(), std::invalid_argument);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue