Xuanwo opened a new pull request, #8868:
URL: https://github.com/apache/opendal/pull/8868
# Which issue does this PR close?
N/A.
# Rationale for this change
`opendal::ReaderStream::ReaderStreamBuf` had three position tracking bugs:
1. `seekoff()` with `std::ios_base::cur` and a non-zero offset delegated
directly to `reader_.Seek(off, dir)`. Because `underflow()` reads ahead up to
8192 bytes into `buffer_`, relative seeks applied to the read-ahead `reader_`
position instead of the logical stream position `gptr()`.
2. `underflow()` incremented `buffer_start_pos_` by `egptr() - eback()`
before reading, but returned `traits_type::eof()` on EOF without resetting the
get pointers (`setg(buffer_, buffer_, buffer_)`). Subsequent `tellg()` calls
double-counted the last buffer length, and repeated `underflow()` calls at EOF
kept incrementing `buffer_start_pos_`.
3. The `ReaderStreamBuf` constructor initialized `buffer_start_pos_` to `0`,
losing the real offset when constructed from a `Reader` that had already been
read or seeked.
In addition, `seekoff()` did not check `which & std::ios_base::in` and let
exceptions from `Reader::Seek()` propagate out of `seekoff()`, causing
`std::istream::seekg()` to set `badbit` instead of `failbit` on out-of-range
seeks.
# What changes are included in this PR?
- `bindings/cpp/include/opendal.hpp`:
- Initialize `buffer_start_pos_` from `reader_.Seek(0,
std::ios_base::cur)`.
- Reset the get area (`setg(buffer_, buffer_, buffer_)`) immediately after
advancing `buffer_start_pos_` in `underflow()`, keeping the buffer empty and
position accurate on EOF or read failure.
- Fix `seekoff()` to require `which & std::ios_base::in`, compute logical
`std::ios_base::cur` targets with signed overflow/underflow checks, satisfy
in-buffer `beg`/`cur` seeks directly via `setg()`, seek out-of-buffer targets
via `std::ios_base::beg`, catch invalid `Reader::Seek` errors (and reject
`std::streamoff`-overflowing `end` seeks without desyncing `reader_`), and
return `pos_type(off_type(-1))` so `istream::seekg()` sets `failbit` without
corrupting stream state.
- `bindings/cpp/tests/basic_test.cpp`:
- Add deterministic unit tests covering in-buffer forward/backward
`std::ios::cur` seeks, `tellg()` and repeated `underflow()`/`peek()`/`get()` at
EOF (including empty files), constructing `ReaderStream` from an already-read
or seeked `Reader`, seeks across the 8192-byte buffer boundary,
`beg`/`end`/`seekpos` seeks, output-only `pubseekoff`, and invalid
negative/overflow seeks.
# Are there any user-facing changes?
`opendal::ReaderStream` now reports accurate `tellg()` positions at EOF and
when wrapping an already-positioned `Reader`, seeks relative to the logical
stream position on `std::ios::cur`, and sets `failbit` without corrupting
buffered state on invalid seeks.
# Breaking changes
# AI Usage Statement
- Harness: pi / T3 Code
- Model: Gemini 4 Neon
- Effort: High
- Role: Investigated `ReaderStreamBuf` and `Reader::Seek`, implemented the
header fix and regression tests, verified tests fail before the fix and pass
after with ASan/UBSan/LSan on GCC and Clang, and opened this PR.
--
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]