util: get number of bytes consumed from buffer by LineReader
What changed, and why it matters
This commit adds a simple helper method called Consumed() to a utility class named LineReader. It tells callers how many bytes have already been read from an input buffer. The change is purely informational and includes new unit tests to verify the counts. There is no security fix or behavior change to how data is parsed.
No security action needed. Treat as a normal code-quality/testability change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces LineReader::Consumed(), implemented as std::distance(start, it), and exposes it in the header. It also adds BOOST_CHECK_EQUAL assertions in the existing line_reader_test to confirm that Consumed() and Remaining() track correctly across reads. No parsing logic, bounds handling, or error paths are modified.
Changed components
src/util/string.hsrc/util/string.cppsrc/test/util_string_tests.cppInspect captured patch +16 / −0
diff --git a/src/test/util_string_tests.cpp b/src/test/util_string_tests.cpp
index 77576950..ad54db45 100644
--- a/src/test/util_string_tests.cpp
+++ b/src/test/util_string_tests.cpp
@@ -195,9 +195,13 @@ BOOST_AUTO_TEST_CASE(line_reader_test)
// Check three lines terminated by \n and \r\n, trimming whitespace
const std::vector<std::byte> input{StringToBuffer("once upon a time\n there was a dog \r\nwho liked food\n")};
LineReader reader(input, /*max_line_length=*/128);
+ BOOST_CHECK_EQUAL(reader.Consumed(), 0);
+ BOOST_CHECK_EQUAL(reader.Remaining(), 51);
std::optional<std::string> line1{reader.ReadLine()};
+ BOOST_CHECK_EQUAL(reader.Consumed(), 17);
BOOST_CHECK_EQUAL(reader.Remaining(), 34);
std::optional<std::string> line2{reader.ReadLine()};
+ BOOST_CHECK_EQUAL(reader.Consumed(), 36);
BOOST_CHECK_EQUAL(reader.Remaining(), 15);
std::optional<std::string> line3{reader.ReadLine()};
std::optional<std::string> line4{reader.ReadLine()};
@@ -208,6 +212,8 @@ BOOST_AUTO_TEST_CASE(line_reader_test)
BOOST_CHECK_EQUAL(line1.value(), "once upon a time");
BOOST_CHECK_EQUAL(line2.value(), "there was a dog");
BOOST_CHECK_EQUAL(line3.value(), "who liked food");
+ BOOST_CHECK_EQUAL(reader.Consumed(), 51);
+ BOOST_CHECK_EQUAL(reader.Remaining(), 0);
}
{
// Do not exceed max_line_length + 1 while searching for \n
diff --git a/src/util/string.cpp b/src/util/string.cpp
index 6e86095a..d1ef7a18 100644
--- a/src/util/string.cpp
+++ b/src/util/string.cpp
@@ -70,4 +70,9 @@ size_t LineReader::Remaining() const
{
return std::distance(it, end);
}
+
+size_t LineReader::Consumed() const
+{
+ return std::distance(start, it);
+}
} // namespace util
diff --git a/src/util/string.h b/src/util/string.h
index 20c78583..64b49bb0 100644
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -295,6 +295,11 @@ struct LineReader {
* Returns remaining size of bytes in buffer
*/
size_t Remaining() const;
+
+ /**
+ * Returns number of bytes already read from buffer
+ */
+ size_t Consumed() const;
};
} // namespace util
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.