RFR: 8305650: os::lasterror incorrectly truncates buffer in os_windows.cpp
David Holmes
dholmes at openjdk.org
Wed Apr 5 21:52:14 UTC 2023
On Wed, 5 Apr 2023 13:48:22 GMT, Julian Waters <jwaters at openjdk.org> wrote:
>> 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 :)
>
> Nevermind, just checked the POSIX version and it also has this flaw, so it's not just Windows
We have a buffer `buf` of length `len` into which we need to copy a string `s` of length `n` and nul-terminate it. So the string will occupy `buf[0]` to `buf[n-1]` with the nul at `buf[n]`.
If the string + nul is too big for the buffer `n >= len` then we set `n = len - 1` so we copy a truncated `s` and then store the nul at `buf[n]`.
There is no bug here.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/13350#discussion_r1159059380
More information about the hotspot-runtime-dev
mailing list