The encodedLengthUTF8() method uses an int accumulator (dp) for the LATIN1 code 
path, while the UTF16 path (encodedLengthUTF8_UTF16) correctly uses a long 
accumulator with an overflow check. When a LATIN1 string contains more than 
Integer.MAX_VALUE/2 non-ASCII bytes, the int dp overflows, potentially causing 
NegativeArraySizeException in downstream buffer allocation.

Fix: change dp from int to long and add the same overflow check used in the 
UTF16 path.

-------------

Commit messages:
 - Improve test: use encodedLength() directly and increase memory
 - Fix integer overflow in String.encodedLengthUTF8 LATIN1 path

Changes: https://git.openjdk.org/jdk/pull/30189/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=30189&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8379786
  Stats: 138 lines in 2 files changed: 135 ins; 0 del; 3 mod
  Patch: https://git.openjdk.org/jdk/pull/30189.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/30189/head:pull/30189

PR: https://git.openjdk.org/jdk/pull/30189

Reply via email to