[12] RFR: 8214777: Avoid some GCC 8.X strncpy() errors in HotSpot
Andrew Haley
aph at redhat.com
Tue Dec 11 09:44:03 UTC 2018
On 12/11/18 1:27 AM, Kim Barrett wrote:
>> On Dec 10, 2018, at 1:57 PM, Simon Tooke <stooke at redhat.com> wrote:
>>
>> This small patch fixes some simple warnings in Hotspot code, found by
>> GCC 8.1
>>
>> Essentially, any code sequence of the pattern
>>
>> int l = strlen(somestring)
>> char* buffer = malloc(l + 1)
>> strncpy(buffer, somestring, l)
>> buffer[l] = 0
>>
>> is replaced by
>>
>> int len = strlen(somestring)
>> char* buffer = malloc(len + 1)
>> strncpy(buffer, somestring, len + 1)
>>
>> For xmlstream.cpp, this is actually a small inefficiency, as the null
>> byte is immediately overwritten; but it makes GCC happy.
>
> Why not just call strcpy, rather than strncpy, since the size is obviously sufficient.
I would advise against that. The word "obvious" in this context always makes me
nervous because many bugs have been written when the programmer thought something
was obvious.
I'd be happier if there were no calls to strcpy() anywhere. Any usage of it is
a red flag.
--
Andrew Haley
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671
More information about the hotspot-runtime-dev
mailing list