Copilot commented on code in PR #3859:
URL: https://github.com/apache/avro/pull/3859#discussion_r3568231175


##########
lang/c++/include/avro/Stream.hh:
##########
@@ -345,6 +356,30 @@ struct StreamReader {
         return next_ != end_ || fill();
     }
 
+    /**
+     * Returns the number of bytes still available to be read: those already
+     * buffered in this reader plus whatever the underlying stream reports as
+     * remaining. Returns a negative value when the underlying stream cannot
+     * report its remaining size.
+     */
+    int64_t remainingBytes() const {
+        if (in_ == nullptr) {
+            return -1;
+        }
+        int64_t streamRemaining = in_->remainingBytes();
+        if (streamRemaining < 0) {
+            return -1;
+        }
+        // Bytes already buffered in this reader, added to what the underlying
+        // stream still has. next_ and end_ can both be null right after
+        // init()/reset() (before any data is buffered); subtracting two null
+        // pointers is undefined behavior, so treat equal pointers (including 
the
+        // both-null case) as zero buffered and only subtract real buffer
+        // pointers. Pointer equality, unlike subtraction, is well-defined 
here.
+        int64_t buffered = (next_ == end_) ? 0 : (end_ - next_);
+        return buffered + streamRemaining;

Review Comment:
   StreamReader::remainingBytes() only treats the both-null case as zero 
buffered via (next_ == end_), but if either pointer is null while the other is 
not (e.g., after reset() or a failed/partial fill), the expression (end_ - 
next_) would still be evaluated and is undefined behavior. Since this method is 
meant to be safe to call right after init/reset, it should defensively treat 
any null pointer as zero buffered.



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

Reply via email to