RFR: 8305650: os::lasterror incorrectly truncates buffer in os_windows.cpp
Julian Waters
jwaters at openjdk.org
Wed Apr 5 13:41:20 UTC 2023
On Wed, 5 Apr 2023 13:34:32 GMT, Julian Waters <jwaters at openjdk.org> wrote:
>> 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
Unless os::lasterror's len parameter is not supposed to include space for the null terminator, but I couldn't find any documentation or comment that says so. But if that were the case, then this whole code segment would be wrong, not just my change
Ahh, the joys of working with Windows error handling :)
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/13350#discussion_r1158533914
More information about the hotspot-runtime-dev
mailing list