You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "garydgregory (via GitHub)" <gi...@apache.org> on 2025/12/07 15:39:54 UTC
[PR] SeekableInMemoryByteChannel.position(long) shouldn't throw an IllegalArgumentException for a new positive position that's too large [commons-compress]
garydgregory opened a new pull request, #756:
URL: https://github.com/apache/commons-compress/pull/756
SeekableInMemoryByteChannel.position(long) shouldn't throw an IllegalArgumentException for a new positive position that's too large
Before you push a pull request, review this list:
- [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
- [x] Read the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) if you use Artificial Intelligence (AI).
- [ ] I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
- [x] Run a successful build using the default [Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command line by itself.
- [x] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
- [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
- [x] Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.
--
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: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] SeekableInMemoryByteChannel.position(long) shouldn't throw an IllegalArgumentException for a new positive position that's too large [commons-compress]
Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory merged PR #756:
URL: https://github.com/apache/commons-compress/pull/756
--
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: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] SeekableInMemoryByteChannel.position(long) shouldn't throw an IllegalArgumentException for a new positive position that's too large [commons-compress]
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #756:
URL: https://github.com/apache/commons-compress/pull/756#discussion_r2599029596
##########
src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java:
##########
@@ -160,36 +163,41 @@ public long size() throws ClosedChannelException {
@Override
public SeekableByteChannel truncate(final long newSize) throws ClosedChannelException {
ensureOpen();
- if (newSize < 0L || newSize > Integer.MAX_VALUE) {
- throw new IllegalArgumentException("Size must be range [0.." + Integer.MAX_VALUE + "]");
+ if (newSize < 0L) {
+ throw new IllegalArgumentException(String.format("New size is negative: %,d", newSize));
}
if (size > newSize) {
size = (int) newSize;
}
if (position > newSize) {
- position = (int) newSize;
+ position = newSize;
}
return this;
}
@Override
public int write(final ByteBuffer b) throws IOException {
ensureOpen();
+ if (position > Integer.MAX_VALUE) {
+ throw new IOException("position > Integer.MAX_VALUE");
+ }
int wanted = b.remaining();
- final int possibleWithoutResize = size - position;
+ final int intPos = (int) position;
+ final int possibleWithoutResize = size - intPos;
if (wanted > possibleWithoutResize) {
- final int newSize = position + wanted;
+ final int newSize = intPos + wanted;
if (newSize < 0) { // overflow
resize(Integer.MAX_VALUE);
- wanted = Integer.MAX_VALUE - position;
+ wanted = Integer.MAX_VALUE - intPos;
} else {
resize(newSize);
}
}
- b.get(data, position, wanted);
+ b.get(data, intPos, wanted);
position += wanted;
- if (size < position) {
- size = position;
+ final int intPos2 = (int) position;
+ if (size < intPos2) {
+ size = intPos2;
Review Comment:
_Nit_: What about reusing `intPos`? Also a comment might be useful to explain that `intPos + wanted` is at most `Integer.MAX_VALUE - intPos + intPos`.
```java
position = intPos += wanted;
if (size < intPos) {
size = intPos;
```
--
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: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] SeekableInMemoryByteChannel.position(long) shouldn't throw an IllegalArgumentException for a new positive position that's too large [commons-compress]
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #756:
URL: https://github.com/apache/commons-compress/pull/756#discussion_r2599029596
##########
src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java:
##########
@@ -160,36 +163,41 @@ public long size() throws ClosedChannelException {
@Override
public SeekableByteChannel truncate(final long newSize) throws ClosedChannelException {
ensureOpen();
- if (newSize < 0L || newSize > Integer.MAX_VALUE) {
- throw new IllegalArgumentException("Size must be range [0.." + Integer.MAX_VALUE + "]");
+ if (newSize < 0L) {
+ throw new IllegalArgumentException(String.format("New size is negative: %,d", newSize));
}
if (size > newSize) {
size = (int) newSize;
}
if (position > newSize) {
- position = (int) newSize;
+ position = newSize;
}
return this;
}
@Override
public int write(final ByteBuffer b) throws IOException {
ensureOpen();
+ if (position > Integer.MAX_VALUE) {
+ throw new IOException("position > Integer.MAX_VALUE");
+ }
int wanted = b.remaining();
- final int possibleWithoutResize = size - position;
+ final int intPos = (int) position;
+ final int possibleWithoutResize = size - intPos;
if (wanted > possibleWithoutResize) {
- final int newSize = position + wanted;
+ final int newSize = intPos + wanted;
if (newSize < 0) { // overflow
resize(Integer.MAX_VALUE);
- wanted = Integer.MAX_VALUE - position;
+ wanted = Integer.MAX_VALUE - intPos;
} else {
resize(newSize);
}
}
- b.get(data, position, wanted);
+ b.get(data, intPos, wanted);
position += wanted;
- if (size < position) {
- size = position;
+ final int intPos2 = (int) position;
+ if (size < intPos2) {
+ size = intPos2;
Review Comment:
_Nit_: What about reusing `intPos`? Also a comment might be useful to explain that `intPos + wanted` is at most `(Integer.MAX_VALUE - intPos) + intPos`.
```java
position = intPos += wanted;
if (size < intPos) {
size = intPos;
```
--
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: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] SeekableInMemoryByteChannel.position(long) shouldn't throw an IllegalArgumentException for a new positive position that's too large [commons-compress]
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #756:
URL: https://github.com/apache/commons-compress/pull/756#discussion_r2598458600
##########
src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java:
##########
@@ -113,8 +113,8 @@ public long position() throws ClosedChannelException {
@Override
public SeekableByteChannel position(final long newPosition) throws IOException {
ensureOpen();
- if (newPosition < 0L || newPosition > Integer.MAX_VALUE) {
- throw new IllegalArgumentException(String.format("Position must be in range [0..%,d]: %,d", Integer.MAX_VALUE, newPosition));
+ if (newPosition < 0L) {
+ throw new IllegalArgumentException(String.format("New position is negative: %,d", newPosition));
}
position = (int) newPosition;
Review Comment:
I am afraid that after this change, the `position` field must be promoted to a `long` to prevent an integer overflow.
By removing the check `position > Integer.MAX_VALUE` here, we should introduce it in:
- At the beginning of `read`: if `position` is greater than `Integer.MAX_VALUE` then `-1` is immediately returned.
- At the beginning of `write`: if `position` is greater than `Integer.MAX_VALUE` a `IOException` must be thrown.
Moreover, I saw a related bug in `truncate`: if the argument is more than `Integer.MAX_VALUE`, the method should be a no-op.
##########
src/test/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannelTest.java:
##########
@@ -152,9 +152,11 @@ void testShouldThrowExceptionOnWritingToClosedChannel() {
}
@Test
- void testShouldThrowExceptionWhenSettingIncorrectPosition() {
+ void testShouldThrowExceptionWhenSettingIncorrectPosition() throws IOException {
Review Comment:
Should we have different tests here?
- If the argument is `-1`, an exception should be thrown,
- If the argument is `2L << 32 - 1L` (same as `-1` when cast to an `int`), no exception should be thrown.
--
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: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] SeekableInMemoryByteChannel.position(long) shouldn't throw an IllegalArgumentException for a new positive position that's too large [commons-compress]
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #756:
URL: https://github.com/apache/commons-compress/pull/756#discussion_r2599999011
##########
src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java:
##########
@@ -160,36 +163,42 @@ public long size() throws ClosedChannelException {
@Override
public SeekableByteChannel truncate(final long newSize) throws ClosedChannelException {
ensureOpen();
- if (newSize < 0L || newSize > Integer.MAX_VALUE) {
- throw new IllegalArgumentException("Size must be range [0.." + Integer.MAX_VALUE + "]");
+ if (newSize < 0L) {
+ throw new IllegalArgumentException(String.format("New size is negative: %,d", newSize));
}
if (size > newSize) {
size = (int) newSize;
}
if (position > newSize) {
- position = (int) newSize;
+ position = newSize;
}
return this;
}
@Override
public int write(final ByteBuffer b) throws IOException {
ensureOpen();
+ if (position > Integer.MAX_VALUE) {
+ throw new IOException("position > Integer.MAX_VALUE");
+ }
int wanted = b.remaining();
- final int possibleWithoutResize = size - position;
+ // intPos <= Integer.MAX_VALUE
+ int intPos = (int) position;
+ final int possibleWithoutResize = size - intPos;
if (wanted > possibleWithoutResize) {
- final int newSize = position + wanted;
+ final int newSize = intPos + wanted;
if (newSize < 0) { // overflow
resize(Integer.MAX_VALUE);
- wanted = Integer.MAX_VALUE - position;
+ wanted = Integer.MAX_VALUE - intPos;
} else {
Review Comment:
I didn't catch it before, because I didn't have the Javadoc in mind, however the generic [`WritableByteChannel#write` Javadoc](https://docs.oracle.com/javase/8/docs/api/java/nio/channels/WritableByteChannel.html) says:
> Unless otherwise specified, a write operation will return only after writing all of the `r` requested bytes.
So this edge case should throw instead of decreasing `wanted`. Do you agree?
--
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: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] SeekableInMemoryByteChannel.position(long) shouldn't throw an IllegalArgumentException for a new positive position that's too large [commons-compress]
Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on code in PR #756:
URL: https://github.com/apache/commons-compress/pull/756#discussion_r2599127727
##########
src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java:
##########
@@ -160,36 +163,41 @@ public long size() throws ClosedChannelException {
@Override
public SeekableByteChannel truncate(final long newSize) throws ClosedChannelException {
ensureOpen();
- if (newSize < 0L || newSize > Integer.MAX_VALUE) {
- throw new IllegalArgumentException("Size must be range [0.." + Integer.MAX_VALUE + "]");
+ if (newSize < 0L) {
+ throw new IllegalArgumentException(String.format("New size is negative: %,d", newSize));
}
if (size > newSize) {
size = (int) newSize;
}
if (position > newSize) {
- position = (int) newSize;
+ position = newSize;
}
return this;
}
@Override
public int write(final ByteBuffer b) throws IOException {
ensureOpen();
+ if (position > Integer.MAX_VALUE) {
+ throw new IOException("position > Integer.MAX_VALUE");
+ }
int wanted = b.remaining();
- final int possibleWithoutResize = size - position;
+ final int intPos = (int) position;
+ final int possibleWithoutResize = size - intPos;
if (wanted > possibleWithoutResize) {
- final int newSize = position + wanted;
+ final int newSize = intPos + wanted;
if (newSize < 0) { // overflow
resize(Integer.MAX_VALUE);
- wanted = Integer.MAX_VALUE - position;
+ wanted = Integer.MAX_VALUE - intPos;
} else {
resize(newSize);
}
}
- b.get(data, position, wanted);
+ b.get(data, intPos, wanted);
position += wanted;
- if (size < position) {
- size = position;
+ final int intPos2 = (int) position;
+ if (size < intPos2) {
+ size = intPos2;
Review Comment:
Done.
--
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: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org