diff --git a/components/core/src/clp/CheckpointReader.cpp b/components/core/src/clp/CheckpointReader.cpp index a93dcb50f..4e2fdd606 100644 --- a/components/core/src/clp/CheckpointReader.cpp +++ b/components/core/src/clp/CheckpointReader.cpp @@ -3,8 +3,7 @@ namespace clp { auto CheckpointReader::try_seek_from_begin(size_t pos) -> ErrorCode { m_cur_pos = pos > m_checkpoint ? m_checkpoint : pos; - if (auto const rc = m_reader->try_seek_from_begin(m_cur_pos); - ErrorCode_Success != rc) { + if (auto const rc = m_reader->try_seek_from_begin(m_cur_pos); ErrorCode_Success != rc) { return rc; } if (m_cur_pos >= m_checkpoint) { @@ -18,7 +17,7 @@ auto CheckpointReader::try_read(char* buf, size_t num_bytes_to_read, size_t& num if (m_cur_pos == m_checkpoint) { return ErrorCode_EndOfFile; } - + if ((m_cur_pos + num_bytes_to_read) > m_checkpoint) { num_bytes_to_read = m_checkpoint - m_cur_pos; } diff --git a/components/core/src/clp/CheckpointReader.hpp b/components/core/src/clp/CheckpointReader.hpp index 051e1bdcd..c90801862 100644 --- a/components/core/src/clp/CheckpointReader.hpp +++ b/components/core/src/clp/CheckpointReader.hpp @@ -19,9 +19,10 @@ class CheckpointReader : public ReaderInterface { explicit CheckpointReader(ReaderInterface* reader, size_t checkpoint) : m_reader(reader), m_checkpoint(checkpoint) { - if (nullptr != m_reader) { - m_cur_pos = m_reader->get_pos(); + if (nullptr == m_reader) { + throw ReaderInterface::OperationFailed(ErrorCode_BadParam, __FILE__, __LINE__); } + m_cur_pos = m_reader->get_pos(); if (m_cur_pos > m_checkpoint) { throw ReaderInterface::OperationFailed(ErrorCode_BadParam, __FILE__, __LINE__); } diff --git a/components/core/tests/test-CheckpointReader.cpp b/components/core/tests/test-CheckpointReader.cpp index 116759568..216a423a0 100644 --- a/components/core/tests/test-CheckpointReader.cpp +++ b/components/core/tests/test-CheckpointReader.cpp @@ -43,6 +43,11 @@ TEST_CASE("Test Checkpoint Reader", "[CheckpointReader]") { REQUIRE(1 == num_bytes_read); REQUIRE(1 == string_reader.get_pos()); REQUIRE(1 == checkpoint_reader.get_pos()); + rc = checkpoint_reader.try_read(buf, 1, num_bytes_read); + REQUIRE(clp::ErrorCode_EndOfFile == rc); + REQUIRE(0 == num_bytes_read); + REQUIRE(1 == string_reader.get_pos()); + REQUIRE(1 == checkpoint_reader.get_pos()); } SECTION("CheckpointReader does allow reads before checkpoint.") {