RFR: 8325002: Exceptions::fthrow needs to ensure it truncates to a valid utf8 string

Thomas Stuefe stuefe at openjdk.org
Fri Jul 26 05:44:31 UTC 2024


On Fri, 26 Jul 2024 04:03:10 GMT, David Holmes <dholmes at openjdk.org> wrote:

> Exceptions::fthrow uses a 1024 byte buffer to format the incoming exception message string, but this may not be large enough, leading to truncation. However, we should ensure we truncate to a valid UTF8 sequence.
> 
> The process is explained in the code. Thanks to @RogerRiggs and @djelinski for their suggestions on how to tackle this.
> 
> Testing:
>  - new gtest exercises the truncation code with the different possibilities for bad truncation
>  - tiers 1-3 sanity testing
> 
> Thanks.

src/hotspot/share/utilities/exceptions.cpp line 277:

> 275:   if ((ret == -1 || ret >= max_msg_size) && strlen(msg) > 0) {
> 276:     assert(msg[max_msg_size - 1] == '\0', "should be null terminated");
> 277:     UTF8::truncate_to_legal_utf8((unsigned char*)msg, max_msg_size);

Ah, I misread your patch and thought you pass in the strlen of the message to the truncation function, when in fact you pass in the hard coded message buffer size. 

But that begs the question of why you test strlen above, and more importantly, whether all cases where snprintf returns an error are truncation problems. It could have detected an invalid UTF8 sequence and aborted in the middle of it.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/20345#discussion_r1692538448


More information about the hotspot-dev mailing list