wwbmmm commented on code in PR #3686:
URL: https://github.com/apache/brpc/pull/3686#discussion_r4675344233
##########
test/iobuf_unittest.cpp:
##########
@@ -2030,6 +2030,134 @@ static void append_as_separate_blocks(butil::IOBuf* buf,
}
}
+TEST_F(IOBufTest, cutter_remaining_contiguous_bytes_empty_and_append) {
Review Comment:
[replied by brpc-oncall robot] Please add regression tests for the three
scenarios raised in the review — none of the current tests exercise them,
because `append_as_separate_blocks()` uses `append_user_data()` (an isolated
block) instead of `append()` on the shared TLS block, so the in-place reference
merge never happens:
1. `append()` merging into the reference the cutter has already loaded
(plain `buf.append("def")` after the cutter saw `"abc"`), covering both the
untouched-cursor and partially-consumed cases plus the destructor commit;
2. two successive cutters over an IOBuf with more than two references, where
the first commits a shortened front reference — assert
`size()`/`remaining_bytes()` and that the second cutter's `fetch()` reports
insufficient data instead of a short copy;
3. a zero-length reference produced by `appendv()` with an empty iovec —
assert the first call to `remaining_contiguous_bytes()` and `fetch()` already
sees the following data.
##########
src/butil/iobuf.h:
##########
@@ -534,13 +534,29 @@ class IOBufCutter {
// Returns pointer to the character, nullptr on empty
const void* fetch1();
+ // Peek n bytes without consuming them. Return a pointer into the backing
+ // block when contiguous, otherwise copy into aux_buffer (at least n
bytes).
+ // Return nullptr on insufficient data, leaving aux_buffer unchanged.
+ // For n == 0, return aux_buffer without accessing the buffer.
+ const void* fetch(void* aux_buffer, size_t n) const;
+
+ // Access the underlying IOBuf without copying or synchronizing the cursor.
+ // Its front block may still include bytes already consumed by this cutter.
+ const IOBuf& buf() const { return *_buf; }
Review Comment:
[replied by brpc-oncall robot] `buf()` still has no consumer outside these
tests (`buf` is already in scope in every test that uses it), and it exposes
exactly the cursor-inconsistent view this PR is meant to eliminate: the front
reference still contains bytes already consumed by the cutter and, per the
comments above, its length/nbytes may not even be settled. Please drop it and
use the local `buf` in the tests; if it must stay, document that it must not be
used to read pending data nor be modified while the cutter is alive.
##########
src/butil/iobuf.h:
##########
@@ -534,13 +534,29 @@ class IOBufCutter {
// Returns pointer to the character, nullptr on empty
const void* fetch1();
+ // Peek n bytes without consuming them. Return a pointer into the backing
+ // block when contiguous, otherwise copy into aux_buffer (at least n
bytes).
+ // Return nullptr on insufficient data, leaving aux_buffer unchanged.
+ // For n == 0, return aux_buffer without accessing the buffer.
+ const void* fetch(void* aux_buffer, size_t n) const;
Review Comment:
[replied by brpc-oncall robot] Please document the lifetime contract of
`fetch()`: when an internal pointer is returned it takes no extra reference on
the block, so it is invalidated as soon as the IOBuf is modified (appends
included — see the discussions above) or the cutter advances, because
`load_next_ref()`/the destructor pop or rewrite the front reference and can
release the block. `IOBuf::fetch` already documents this, and parsers will
naturally cache the returned pointer, so the contract should be explicit here
too.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]