Skip to content
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

Bugfix: skip in support.io.SkipShieldingInputStream must return non-negative #737

Merged
merged 2 commits into from
Oct 17, 2021

Conversation

dennisnez
Copy link
Contributor

read() in skip(n) sometimes returns -1, but skip cannot return a negative number. This caused an infinite loop in recent versions of commons-compress (IOUtils.java) which introduced another while loop in it's skip function that assumed the overrided skip() function returned a non-negative value.

…ative value

read() in skip(n) sometimes returns -1, but skip cannot return a negative number. This caused an infinite loop in recent versions of commons-compress (IOUtils.java) which introduced another while loop in it's skip function that assumed the overrided skip() function returned a non-negative value.
Copy link
Contributor

@Bombe Bombe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from those two things, nice catch!

@@ -46,6 +46,14 @@ public SkipShieldingInputStream(InputStream in) {

@Override
public long skip(long n) throws IOException {
return n < 0 ? 0 : read(SKIP_BUFFER, 0, (int) Math.min(n, SKIP_BUFFER_SIZE));
int retval;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't add a variable just for the return value; returning the final value from where it's found is preferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the whole point of this patch is to check the return value of that read() call to make sure it's not negative. How can that be done without introducing a return variable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (n < 0) {
  return 0;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, if you need to store a return value, go right ahead but just for the sake of “only have a single exit point” (which is quite stupid)? Please don’t.

@ArneBab ArneBab merged commit 6ea48aa into hyphanet:next Oct 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants