RFR: 8305650: os::lasterror incorrectly truncates buffer in os_windows.cpp

Thomas Stuefe stuefe at openjdk.org
Thu Apr 6 06:03:22 UTC 2023


On Thu, 6 Apr 2023 05:52:35 GMT, David Holmes <dholmes at openjdk.org> wrote:

>> I understand that that would be the case if n = strlen(s) + 1, but here it's only n = strlen(s). Am I missing something?
>
> char* s = "A";
> int n = strlen(s); // n == 1
> char buf[2];
> strncpy(buf, s, n)  // copies 1 character:  buf[0] == 'A'
> buf[n] = '\0`;    // buf[1] == '0`

> I understand that that would be the case if n = strlen(s) + 1, but here it's only n = strlen(s). Am I missing something?

Array addressing via [] is zero based. See Davids example. For "ABC" A is index 0, C is 2, \0 is 3.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/13350#discussion_r1159315122


More information about the hotspot-runtime-dev mailing list