RFR: 8321180: Condition for non-latin1 string size too large exception is off by one [v2]

Raffaello Giulietti rgiulietti at openjdk.org
Fri Dec 8 20:26:17 UTC 2023


On Fri, 8 Dec 2023 19:50:30 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

>> In the compact string implementation of non-latin1 (UTF16) strings the length is constrained by VM implementation limit on the size a byte array that can be allocated. To produce a useful exception the implementation checks the string size against the maximum byte array size. The exception message is correct
>> 
>> "UTF16 String size is " + len + ", should be less than or equal to " + MAX_LENGTH
>> 
>> The code should match the message, otherwise the exception thrown is: 
>> 
>> java.lang.OutOfMemoryError: Requested array size exceeds VM limit
>
> Roger Riggs has updated the pull request incrementally with one additional commit since the last revision:
> 
>   As suggested by reviewers
>   Correct exception message text
>   Removed unused imports

test/jdk/java/lang/String/CompactString/MaxSizeUTF16String.java line 53:

> 51:         int nonAsciiSize = nonAscii.length;
> 52:         int asciisize = byteSize - nonAsciiSize;
> 53:         byte[] arr = new byte[asciisize + nonAsciiSize];

Suggestion:

        byte[] arr = new byte[byteSize];

Note that `asciisize + nonAsciiSize` = `byteSize - nonAsciiSize + nonAsciiSize` = `byteSize`, even in the presence of overflows. Then `asciisize` becomes useless.

test/jdk/java/lang/String/CompactString/MaxSizeUTF16String.java line 54:

> 52:         int asciisize = byteSize - nonAsciiSize;
> 53:         byte[] arr = new byte[asciisize + nonAsciiSize];
> 54:         Arrays.fill(arr, (byte)'x');      // fill with latin1

`(byte) 0` is a valid UTF-8 sequence after all, so there is no need to fill the array with `'x'`.

test/jdk/java/lang/String/CompactString/MaxSizeUTF16String.java line 64:

> 62:         int nonAsciiSize = nonAscii.length;
> 63:         int asciisize = size - nonAsciiSize;
> 64:         char[] arr = new char[asciisize + nonAsciiSize];

Same as above

test/jdk/java/lang/String/CompactString/MaxSizeUTF16String.java line 65:

> 63:         int asciisize = size - nonAsciiSize;
> 64:         char[] arr = new char[asciisize + nonAsciiSize];
> 65:         Arrays.fill(arr, 'x');      // fill with latin1

Same as above

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

PR Review Comment: https://git.openjdk.org/jdk/pull/17008#discussion_r1420993528
PR Review Comment: https://git.openjdk.org/jdk/pull/17008#discussion_r1420993636
PR Review Comment: https://git.openjdk.org/jdk/pull/17008#discussion_r1420993740
PR Review Comment: https://git.openjdk.org/jdk/pull/17008#discussion_r1420993780


More information about the core-libs-dev mailing list