RFR(xs): 8076475: Misuses of strncpy/strncat
Kim Barrett
kim.barrett at oracle.com
Thu Apr 9 13:13:35 UTC 2015
On Apr 8, 2015, at 8:40 AM, Dmitry Samersoff <dmitry.samersoff at oracle.com> wrote:
>
> To make this code nice-looking it's better to add:
>
> if (strlen(libname) > sizeof(newlib->name)) {
> // Bail out with debug message
> ...
> }
> strcpy(newlib->name, libname);
>
> rather than use snprintf here.
Just happened to see this going by in my email stream…
There’s a problem with that suggestion: strlen can run off the end if not NUL terminated.
This can crash if, for example, unmapped memory is hit before a NUL is encountered.
This is the sort of thing the strnlen function exists for. Not sure how portably available
that function is though - glibc requires feature test macros to enable it.
More information about the hotspot-runtime-dev
mailing list