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

David Holmes dholmes at openjdk.org
Tue Jul 30 04:19:32 UTC 2024


On Mon, 29 Jul 2024 23:16:14 GMT, David Holmes <dholmes at openjdk.org> wrote:

>> src/hotspot/share/utilities/exceptions.cpp line 275:
>> 
>>> 273:   // we may also have a truncated UTF-8 sequence. In such cases we need to fix the buffer so the UTF-8
>>> 274:   // sequence is valid.
>>> 275:   if ((ret == -1 || ret >= max_msg_size) && strlen(msg) > 0) {
>> 
>> Do we need to check if `strlen(msg) == max_msg_size - 1`? If strlen is shorter, the bytes between the null terminator and max_msg_size are undefined, which might trigger an assertion while truncating.
>
> In fact we know it may be shorter than `max_msg_size - 1` - that is what we get on macOS if the string is huge and exceeds `INT_MAX` causing `vsnprintf` to return -1. I originally had an assert that failed due to that.
> 
> I need to fix this case as well. <sigh>. Good catch.

I did some experimentation here and it seems in practice that if the buffer is only partially filled then it will contain valid data as `vsnprintf` would stop filling at a well-defined point. But as it is not a clearly specified area we pass the buffer through anyway, using `strlen(msg)` . Most of the time a partially filled buffer will end with an ASCII character anyway and so the utf8 truncation operation will be a no-op.

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

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


More information about the hotspot-dev mailing list