Xuanwo opened a new pull request, #8772:
URL: https://github.com/apache/opendal/pull/8772
# Which issue does this PR close?
None (two FFI safety bugs found during maintenance).
# Rationale for this change
Two FFI boundary issues existed in `bindings/cpp`:
1. `opendal::Reader::Read(void *s, std::streamsize n)` and
`opendal::Reader::ReadAt(void *s, std::streamsize n, uint64_t offset)`
constructed a `rust::Slice<uint8_t>` directly from a signed `std::streamsize`
and unchecked pointer. Passing a negative length `n < 0` implicitly converted
to a huge `size_t`, and passing `s == nullptr` with `n > 0` produced a
non-empty null slice across the Rust FFI boundary, violating Rust's `&mut [u8]`
validity invariants.
2. `bindings/cpp/src/async.rs` computed `start..(start + len)` in
`reader_read` with unchecked `u64` addition, causing an arithmetic overflow
panic in debug builds or wrap-around in release builds when `start + len >
u64::MAX`.
# What changes are included in this PR?
- `bindings/cpp/src/reader.cpp`: Validate arguments in `Reader::Read` and
`Reader::ReadAt` before constructing `rust::Slice<uint8_t>`:
- Throw `std::invalid_argument` when `n < 0`, when `n > 0 && s ==
nullptr`, or when `n` exceeds `std::ptrdiff_t` maximum on platforms where
`std::streamsize` is wider.
- Return `0` directly when `n == 0` without crossing FFI with a
potentially null pointer.
- `bindings/cpp/src/async.rs`: Replace `start + len` with
`start.checked_add(len)` in `reader_read` and return a `CxxAsyncException` on
overflow.
- `bindings/cpp/tests/basic_test.cpp` & `bindings/cpp/tests/async_test.cpp`:
Add regression tests covering negative lengths, null buffers, zero-length
reads, and overflowing async reader ranges.
# Are there any user-facing changes?
- `opendal::Reader::Read` and `opendal::Reader::ReadAt` now throw
`std::invalid_argument` on negative lengths or null non-empty buffers and
return `0` for zero-length reads.
- `opendal::async::Reader::Read` now throws a C++ exception instead of
panicking or wrapping when `start + len` overflows `uint64_t`.
# Breaking changes
# AI Usage Statement
- Harness: pi coding agent
- Model: Gemini 4 Neon
- Effort: high
- Role: Audited `bindings/cpp` FFI boundaries, implemented the argument
validation and checked addition fixes, added sync and async C++ regression
tests, ran local Rust and C++ (sync and async with ASan) verification, and
prepared the PR. Both bugs were identified during maintenance.
--
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]