Xuanwo opened a new pull request, #8932:
URL: https://github.com/apache/opendal/pull/8932
# Which issue does this PR close?
N/A
# Rationale for this change
`OpenDal::IO` had four bugs in its Rust implementation under
`bindings/ruby/src/io.rs`:
1. `IO#read(size)` pre-allocated `vec![0; size as usize]` before reading.
Passing a large size such as `2**62` triggered Rust's allocation error abort
(`SIGABRT`), terminating the Ruby interpreter instead of bounding allocation to
available bytes.
2. `IO#write` converted its input via Magnus's `String` (requiring UTF-8)
and `IO#readline` used `BufRead::read_line` into a UTF-8 `String`, causing
binary / `ASCII-8BIT` data (including bytes returned by `IO#read`) to fail with
encoding or UTF-8 errors.
3. `IO#read(size, outbuf)` returned `nil` at EOF without clearing `outbuf`,
leaving stale data in the caller-supplied buffer instead of matching Ruby's
`IO#read` contract.
4. `IO#seek` computed relative targets with unchecked `-offset` and
`position as i64 + offset` arithmetic, panicking (raising an uncatchable
`fatal` error) in debug builds or wrapping silently in release builds on
`i64::MIN` or large positive seeks, and discarded the final `reader.seek`
result.
# What changes are included in this PR?
- `bindings/ruby/src/io.rs`:
- Replace upfront `vec![0; size as usize]` allocation in `Io::read` with
`reader.by_ref().take(size as u64).read_to_end(&mut bs)` so memory usage is
bounded by actual bytes read while preserving `nil` at EOF and `""` for
zero-length reads.
- Clear `outbuf` via `drop_bytes` when `Io::read` returns `nil` at EOF.
- Change `Io::write` to accept `RString` and write `bs.to_bytes()`
(matching `Operator#write`), and change `Io::readline` to read raw bytes with
`BufRead::read_until(b'\n', &mut Vec<u8>)` and return
`ruby.str_from_slice(&buffer)` while preserving `EOFError` at EOF.
- Use checked `i128` arithmetic and `i64::try_from` bounds checks in
`Io::seek` for `SEEK_CUR` and `SEEK_END`, restore the reader position on
`SEEK_END` range errors, and propagate `reader.seek` errors.
- `bindings/ruby/test/io_test.rb`:
- Add regression tests covering huge-size reads, EOF buffer clearing,
binary write/read round-trips, binary `readline` with `EOFError`, and `seek`
underflow/overflow errors.
# Are there any user-facing changes?
Yes:
- `OpenDal::IO#read(size)` no longer aborts the process on large requested
sizes.
- `OpenDal::IO#read(size, outbuf)` clears `outbuf` when returning `nil` at
EOF.
- `OpenDal::IO#write` and `OpenDal::IO#readline` now handle arbitrary binary
/ `ASCII-8BIT` bytes, and `OpenDal::IO#readline` returns an `ASCII-8BIT`
`String` consistent with `OpenDal::IO#read`.
- `OpenDal::IO#seek` raises a catchable `RuntimeError` on offset underflow
or overflow instead of panicking or wrapping.
# Breaking changes
# AI Usage Statement
- Harness: pi
- Model: Gemini 4 Neon
- Effort: high
- Role: Implemented the four `OpenDal::IO` fixes in
`bindings/ruby/src/io.rs`, added regression tests in
`bindings/ruby/test/io_test.rb`, and verified formatting, clippy, and Ruby test
execution against both debug and release builds.
--
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]