You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Chunhui Liu <le...@gmail.com> on 2026/09/17 09:53:29 UTC

Tomcat 11: HttpParser.readHostIPv4 always throws ArrayIndexOutOfBoundsException during IPv4 host parsing

Hi team:

Recently, we noticed that there are hundreds
of ArrayIndexOutOfBoundsExceptions (and deoptimization events) have been
thrown by `HttpParser.readHostIPv4`. It can be reproduced with the
following test case:

```java

@Test
void debugStandardIpv4_isNumericOnEof() throws IOException {
    HttpParser.readHostIPv4(new StringReader("127.0.0.1"), false);
}

```

The method does not throws the exception directly, but rather swallows it
inside `isNumeric` through the following steps:


   1. All characters are read, reaching EOF and returning -1;
   2. The `isNumeric` check is performed before the `-1` check;
   3. `-1` triggers an ArrayIndexOutOfBoundsException inside isNumeric;

[image: image.png]
[image: image.png]

So my question is:

1. Should the `-1` branch be placed before the `isNumeric` branch? or is
there another reason for the current implementation?
2. Should check `0 < c < ARRAY_SIZE`?



Best regards,
Chunhui Liu

Re: Tomcat 11: HttpParser.readHostIPv4 always throws ArrayIndexOutOfBoundsException during IPv4 host parsing

Posted by Chunhui Liu <le...@gmail.com>.
Thank you, Mark. 🤝

Chunhui Liu


On 17/09/2026 15:37, Chunhui Liu wrote:
> > Hi Mark:
> >
> > please help review: [perf] Check for EOF before the numeric test in
> > readHostIPv4 by leeychee · Pull Request #1068 · apache/tomcat
> > <https://github.com/apache/tomcat/pull/1068>
>
> Merged around the same time you wrote the above email.
>
> Thanks for the PR.
>
> Mark
>
>

Re: Tomcat 11: HttpParser.readHostIPv4 always throws ArrayIndexOutOfBoundsException during IPv4 host parsing

Posted by Mark Thomas <ma...@apache.org>.
On 17/09/2026 15:37, Chunhui Liu wrote:
> Hi Mark:
> 
> please help review: [perf] Check for EOF before the numeric test in
> readHostIPv4 by leeychee · Pull Request #1068 · apache/tomcat
> <https://github.com/apache/tomcat/pull/1068>

Merged around the same time you wrote the above email.

Thanks for the PR.

Mark


> 
> Best regards
> Chunhui Liu
> 
> 
> Mark Thomas <ma...@apache.org> 于2026年9月17日周四 18:18写道:
> 
>> On 17/09/2026 10:53, Chunhui Liu wrote:
>>> Hi team:
>>>
>>> Recently, we noticed that there are hundreds
>>> of ArrayIndexOutOfBoundsExceptions (and deoptimization events) have been
>>> thrown by `HttpParser.readHostIPv4`. It can be reproduced with the
>>> following test case:
>>>
>>> ```java
>>>
>>> @Test
>>> void debugStandardIpv4_isNumericOnEof() throws IOException {
>>>       HttpParser.readHostIPv4(new StringReader("127.0.0.1"), false);
>>> }
>>>
>>> ```
>>>
>>> The method does not throws the exception directly, but rather swallows it
>>> inside `isNumeric` through the following steps:
>>>
>>>
>>>      1. All characters are read, reaching EOF and returning -1;
>>>      2. The `isNumeric` check is performed before the `-1` check;
>>>      3. `-1` triggers an ArrayIndexOutOfBoundsException inside isNumeric;
>>>
>>> [image: image.png]
>>> [image: image.png]
>>>
>>> So my question is:
>>>
>>> 1. Should the `-1` branch be placed before the `isNumeric` branch? or is
>>> there another reason for the current implementation?
>>> 2. Should check `0 < c < ARRAY_SIZE`?
>>
>> Option 1 is the better choice.
>>
>> Not option 2 since those methods are deliberately designed to be as fast
>> as possible for valid input.
>>
>> PRs welcome.
>>
>> Mark
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat 11: HttpParser.readHostIPv4 always throws ArrayIndexOutOfBoundsException during IPv4 host parsing

Posted by Chunhui Liu <le...@gmail.com>.
Hi Mark:

please help review: [perf] Check for EOF before the numeric test in
readHostIPv4 by leeychee · Pull Request #1068 · apache/tomcat
<https://github.com/apache/tomcat/pull/1068>

Best regards
Chunhui Liu


Mark Thomas <ma...@apache.org> 于2026年9月17日周四 18:18写道:

> On 17/09/2026 10:53, Chunhui Liu wrote:
> > Hi team:
> >
> > Recently, we noticed that there are hundreds
> > of ArrayIndexOutOfBoundsExceptions (and deoptimization events) have been
> > thrown by `HttpParser.readHostIPv4`. It can be reproduced with the
> > following test case:
> >
> > ```java
> >
> > @Test
> > void debugStandardIpv4_isNumericOnEof() throws IOException {
> >      HttpParser.readHostIPv4(new StringReader("127.0.0.1"), false);
> > }
> >
> > ```
> >
> > The method does not throws the exception directly, but rather swallows it
> > inside `isNumeric` through the following steps:
> >
> >
> >     1. All characters are read, reaching EOF and returning -1;
> >     2. The `isNumeric` check is performed before the `-1` check;
> >     3. `-1` triggers an ArrayIndexOutOfBoundsException inside isNumeric;
> >
> > [image: image.png]
> > [image: image.png]
> >
> > So my question is:
> >
> > 1. Should the `-1` branch be placed before the `isNumeric` branch? or is
> > there another reason for the current implementation?
> > 2. Should check `0 < c < ARRAY_SIZE`?
>
> Option 1 is the better choice.
>
> Not option 2 since those methods are deliberately designed to be as fast
> as possible for valid input.
>
> PRs welcome.
>
> Mark
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Tomcat 11: HttpParser.readHostIPv4 always throws ArrayIndexOutOfBoundsException during IPv4 host parsing

Posted by Mark Thomas <ma...@apache.org>.
On 17/09/2026 10:53, Chunhui Liu wrote:
> Hi team:
> 
> Recently, we noticed that there are hundreds
> of ArrayIndexOutOfBoundsExceptions (and deoptimization events) have been
> thrown by `HttpParser.readHostIPv4`. It can be reproduced with the
> following test case:
> 
> ```java
> 
> @Test
> void debugStandardIpv4_isNumericOnEof() throws IOException {
>      HttpParser.readHostIPv4(new StringReader("127.0.0.1"), false);
> }
> 
> ```
> 
> The method does not throws the exception directly, but rather swallows it
> inside `isNumeric` through the following steps:
> 
> 
>     1. All characters are read, reaching EOF and returning -1;
>     2. The `isNumeric` check is performed before the `-1` check;
>     3. `-1` triggers an ArrayIndexOutOfBoundsException inside isNumeric;
> 
> [image: image.png]
> [image: image.png]
> 
> So my question is:
> 
> 1. Should the `-1` branch be placed before the `isNumeric` branch? or is
> there another reason for the current implementation?
> 2. Should check `0 < c < ARRAY_SIZE`?

Option 1 is the better choice.

Not option 2 since those methods are deliberately designed to be as fast 
as possible for valid input.

PRs welcome.

Mark


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org