RFR: 8366421: ModifiedUtf.utfLen may overflow for giant string [v6]

Chen Liang liach at openjdk.org
Mon Sep 22 17:30:57 UTC 2025


On Sun, 21 Sep 2025 00:18:59 GMT, Guanqiang Han <ghan at openjdk.org> wrote:

>> Please review this patch.
>> 
>> **Description:**
>> 
>> Currently, ModifiedUtf.utfLen returns a signed int. For very large strings, this may overflow and produce negative values, leading to incorrect behavior in code that relies on the UTF length. This patch changes the return type to long, which fully resolves the issue and allows safe handling of giant strings.
>> 
>> **Test:**
>> 
>> GHA
>
> Guanqiang Han has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Update and rename TestUtfLen.java to ModifiedUtfTest.java
>   
>   rename test

test/jdk/jdk/internal/util/ModifiedUtfTest.java line 30:

> 28:  * @modules java.base/jdk.internal.classfile.impl
> 29:  *          java.base/jdk.internal.util
> 30:  * @run main/othervm -Xmx4g --add-opens java.base/jdk.internal.classfile.impl=ALL-UNNAMED ModifiedUtfTest

Suggestion:

 * @modules java.base/jdk.internal.classfile.impl:+open
 *          java.base/jdk.internal.util
 * @run main/othervm -Xmx4g ModifiedUtfTest

test/jdk/jdk/internal/util/ModifiedUtfTest.java line 129:

> 127:       objOut.writeUTF(largeString);
> 128:     } catch (Exception e) {
> 129:     } finally {

You should rethrow the exception here in case of failure instead of truncating, and you no longer need the finally block.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/27285#discussion_r2369546515
PR Review Comment: https://git.openjdk.org/jdk/pull/27285#discussion_r2366216969


More information about the core-libs-dev mailing list