RFR: 8355371: NegativeArraySizeException in print methods in IO or System.console() in JShell

Chen Liang liach at openjdk.org
Tue Apr 29 20:56:16 UTC 2025


On Sun, 27 Apr 2025 05:08:50 GMT, Tatsunori Uchino <duke at openjdk.org> wrote:

>> src/jdk.jshell/share/classes/jdk/jshell/execution/impl/ConsoleImpl.java line 400:
>> 
>>> 398:             // Can be negative because directly casted from byte.
>>> 399:             // java.io.OutputStream.write(int b) stipulates "The 24 high-order bits of b are ignored."
>>> 400:             buffer[bp++] = b & 0xff;
>> 
>> I think the cause is that `OutputStream.write(byte[], int, int)` provides negative bytes. I recommend you to update the "can be negative" comment above to be like:
>> 
>> 
>> // Can be negative because widening from byte in write(byte[], int, int).
>> 
>> 
>> Also note, there is another usage of `b` below in `case READ_CHARS ->` but I think it is also a bug; it should be `readInt(1)`.
>
>> I think the cause is that OutputStream.write(byte[], int, int) provides negative bytes.
> 
> I seem to recall that perhaps so. Your suggestion is better.
> 
>> it should be readInt(1).
> 
> Do you mean `int len = readInt(b);`? I will update it but unfortunately I have no idea which is correct. Do you have any idea to test that the fix is correct?
> I could not tell the difference in jshell by playing with some input.

Yep, that readInt is probably fine because the buffer is large enough, plus the result is unused, so there was no observable consequence.

Also in principle, we will not approve patches unless oca is cleared; meanwhile please update the 2024 last updated year in the license header of 2 files to 2025. (I thought this rule was mentioned in the guide, but apparently it wasnt!)

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

PR Review Comment: https://git.openjdk.org/jdk/pull/24897#discussion_r2062337025


More information about the kulla-dev mailing list