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

Unhandled overflow in the concat() method of per-type primitive utility classes #3304

Conversation

ineuwirth
Copy link
Contributor

@ineuwirth ineuwirth commented Oct 20, 2018

The concat() method in the per-type primitive utility classes throws unexpected NegativeArraySizeException or ArrayIndexOutOfBoundsException if the input arrays contain too many elements.

Added test cases to demonstrate both overflow cases. A possible fix is provided.

In this commit, only the module guava and only the utility class Longs are covered along with the unit tests.

See #3303

… unexpected `NegativeArraySizeException` or `ArrayIndexOutOfBoundsException` if the input arrays contain too many elements.

Added test cases to demonstrate both overflow cases. Possible fix is provided.

In this commit only the module `guava` and only the utility class `Longs` is covered along with its unit tests.

See google#3303
for (long[] array : arrays) {
length += array.length;
}
long[] result = new long[length];
checkNoOverflow(length);

Choose a reason for hiding this comment

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

Math.toIntExact?

Copy link
Contributor Author

@ineuwirth ineuwirth Oct 20, 2018

Choose a reason for hiding this comment

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

I preferred to write a custom checker to provide

  • a better description ("the total number of elements in the arrays must fit in an int" instead of "integer overflow")
  • a better exception type (IllegalArgumentException instead of ArithmeticException - I would be really confused seeing ArithmeticException after concatenating long arrays)

… unexpected `NegativeArraySizeException` or `ArrayIndexOutOfBoundsException` if the input arrays contain too many elements.

De-duplicate unit test code.

See google#3303
@cpovirk cpovirk added the P3 label Jan 13, 2020
@kevinb9n kevinb9n removed their assignment Jun 22, 2021
copybara-service bot pushed a commit that referenced this pull request Jun 26, 2023
…nput arrays contain too many elements.

See #3303

Fixes #3304

RELNOTES=n/a
PiperOrigin-RevId: 543414513
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants