chenBright commented on code in PR #3686:
URL: https://github.com/apache/brpc/pull/3686#discussion_r4661317636


##########
src/butil/iobuf_inl.h:
##########
@@ -241,6 +241,42 @@ inline const void* IOBufCutter::fetch1() {
     return _data;
 }
 
+inline size_t IOBufCutter::remaining_contiguous_bytes() {
+    const char* data = static_cast<const char*>(fetch1());
+    if (data == nullptr) {
+        return 0;
+    }
+    const char* end = static_cast<const char*>(_data_end);
+    return static_cast<size_t>(end - data);
+}
+
+inline const void* IOBufCutter::fetch(void* aux_buffer, size_t n) const {
+    if (n == 0) {
+        return aux_buffer;
+    }
+    if (_block == nullptr) {
+        return _buf->fetch(aux_buffer, n);
+    }
+    const size_t size = (const char*)_data_end - (const char*)_data;
+    if (n <= size) {
+        return _data;
+    }
+    if (n > remaining_bytes()) {
+        return nullptr;
+    }
+    // The exhausted front block has not been removed yet. Peeking into the
+    // next block must also remain zero-copy when all n bytes fit there.
+    if (size == 0) {
+        const StringPiece next = _buf->backing_block(1);
+        if (n <= next.size()) {
+            return next.data();
+        }
+    }
+    const size_t offset = _buf->_front_ref().length - size;
+    _buf->copy_to(aux_buffer, n, offset);
+    return aux_buffer;

Review Comment:
   This unconditionally returns `aux_buffer`, but `copy_to()` can perform a 
short copy. For example, with three separate blocks `ab`, `cd`, and `ef`, if a 
previous Cutter queries the contiguous length, consumes `a`, and is destroyed, 
the first reference is shortened while `BigView::nbytes` remains 6. After the 
next Cutter loads the first segment, calling `fetch(aux, 6)` writes only 
`bcdef` but still returns success. Please update the total length when 
committing the consumed prefix of the first reference, so that insufficient 
data is detected before writing to `aux`.



##########
src/butil/iobuf_inl.h:
##########
@@ -241,6 +241,42 @@ inline const void* IOBufCutter::fetch1() {
     return _data;
 }
 
+inline size_t IOBufCutter::remaining_contiguous_bytes() {
+    const char* data = static_cast<const char*>(fetch1());
+    if (data == nullptr) {
+        return 0;
+    }
+    const char* end = static_cast<const char*>(_data_end);
+    return static_cast<size_t>(end - data);
+}
+
+inline const void* IOBufCutter::fetch(void* aux_buffer, size_t n) const {
+    if (n == 0) {
+        return aux_buffer;
+    }
+    if (_block == nullptr) {
+        return _buf->fetch(aux_buffer, n);
+    }
+    const size_t size = (const char*)_data_end - (const char*)_data;
+    if (n <= size) {
+        return _data;
+    }
+    if (n > remaining_bytes()) {
+        return nullptr;

Review Comment:
   `append()` can extend the first reference cached by Cutter in place. If the 
contiguous length is queried for `abc` and `def` is then appended to the same 
block, `size` remains 3 here, and `fetch(aux, 6)` returns `nullptr`. Even if no 
bytes have been consumed, the destructor writes the first reference's length 
back as 3, losing `def`. Please calculate the actual end of the segment from 
the current first reference, and commit only the consumed prefix when switching 
blocks and on destruction.



##########
src/butil/iobuf_inl.h:
##########
@@ -241,6 +241,42 @@ inline const void* IOBufCutter::fetch1() {
     return _data;
 }
 
+inline size_t IOBufCutter::remaining_contiguous_bytes() {
+    const char* data = static_cast<const char*>(fetch1());
+    if (data == nullptr) {
+        return 0;
+    }
+    const char* end = static_cast<const char*>(_data_end);
+    return static_cast<size_t>(end - data);

Review Comment:
   `appendv()` can leave an empty first reference when given a zero-length 
`iovec`. If `abc` is then appended in a separate block, the first call to 
`remaining_contiguous_bytes()` returns 0, while the second returns 3 at the 
same logical position. Callers that treat 0 as the end will miss `abc`. Please 
skip empty references when locating a readable segment, and have `fetch()` use 
the first non-empty reference to determine whether it can return a pointer 
directly.



-- 
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]

Reply via email to