-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Core: Reject negative ByteBuffer stream seeks #17963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import java.nio.ByteBuffer; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
|
|
||
| /** | ||
| * This ByteBufferInputStream does not consume the ByteBuffer being passed in, but will create a | ||
|
|
@@ -82,6 +83,7 @@ public int read(byte[] bytes, int off, int len) throws IOException { | |
|
|
||
| @Override | ||
| public void seek(long newPosition) throws IOException { | ||
| Preconditions.checkArgument(newPosition >= 0, "Position is negative: %s", newPosition); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when can this be negative ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On the base revision, after reading the first byte and then calling The motivation is defensive validation before mutating stream state, consistent with the existing negative-seek guards in |
||
| if (newPosition > length) { | ||
| throw new EOFException( | ||
| String.format("Cannot seek to position after end of file: %s", newPosition)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This throws
IllegalArgumentExceptionfor a negative position, while the check right below throwsEOFExceptionfor past-EOF — so a caller wrappingseek()incatch (IOException)catches one bound but not the other.I'd keep it as-is, since it matches what S3, GCS, ADLS and OSS all do for negative seeks — the asymmetry is the established convention across the family, not something new here. Might be worth documenting the negative-position contract on
SeekableInputStream.seek()(@throws IllegalArgumentException) so it isn't a surprise. wdyt?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree callers should be aware of the unchecked exception, and kept the exception behavior as requested. The shared interface documentation needs to allow for implementation differences: the local file stream delegates to
RandomAccessFile.seek, which rejects negative positions withIOException. A blanket negative-positionIllegalArgumentExceptioncontract onSeekableInputStreamwould therefore need qualification. I have kept this follow-up focused on the requested tests; a shared Javadoc clarification can describe both failure modes separately.