svn commit: r328492 - head/contrib/opie/libopie

Dimitry Andric dim at FreeBSD.org
Sat Jan 27 22:31:14 UTC 2018


On 27 Jan 2018, at 23:20, Ed Schouten <ed at nuxi.nl> wrote:
> 
> 2018-01-27 23:16 GMT+01:00 Pedro F. Giffuni <pfg at freebsd.org>:
>>        char host[sizeof(utmp.ut_host) + 1];
>>        insecure = 1;
>> 
>> -       strncpy(host, utmp.ut_host, sizeof(utmp.ut_host));
>> -       host[sizeof(utmp.ut_host)] = 0;
>> +       strncpy(host, utmp.ut_host, sizeof(host));
> 
> Wait... This may access utmp.ut_host one byte past the end and no
> longer guarantees that host is null-terminated, right?

No, strncpy "copies at most len characters from src into dst".  However,
if the length of the source is equal to or greater than len, the
destination is *not* null terminated.  This is likely why the
"host[sizeof(utmp.ut_host)] = 0;" statement was added.

In any case, this is why strlcpy exists. :)

-Dimitry

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 223 bytes
Desc: Message signed with OpenPGP
URL: <http://lists.freebsd.org/pipermail/svn-src-head/attachments/20180127/f14f5e37/attachment.sig>


More information about the svn-src-head mailing list