RFR: 8305650: os::lasterror incorrectly truncates buffer in os_windows.cpp
Julian Waters
jwaters at openjdk.org
Wed Apr 5 13:37:15 UTC 2023
On Wed, 5 Apr 2023 12:38:39 GMT, David Holmes <dholmes at openjdk.org> wrote:
>> n in os::lasterror defined by os_windows.cpp is only the length of the string's actual character data, not accounting for the null character terminating the string. We should terminate the n + 1 character, not the n'th character
>
> src/hotspot/os/windows/os_windows.cpp line 2153:
>
>> 2151: if (n >= len) n = len - 1;
>> 2152: strncpy(buf, s, n);
>> 2153: buf[n + 1] = '\0';
>
> If n was >= len then you are now indexing outside of buf!
n is always meant to be the length of the buffer data without the null terminator. The check there is meant to enforce exactly that, it sets n = len - 1 because len is a parameter passed to lasterror, and is the size of the buffer the caller handed us. len is supposed to also include space for the null terminator while n is only the length of the actual character data, which is why the - 1 check is there
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/13350#discussion_r1158529442
More information about the hotspot-runtime-dev
mailing list