Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 26 Jan 2019 15:28:22 -0800
From:      Devin Teske <dteske@freebsd.org>
To:        Colin Percival <cperciva@tarsnap.com>
Cc:        rgrimes@freebsd.org, Stefan Esser <se@freebsd.org>, src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   Re: svn commit: r343480 - head/lib/libfigpar
Message-ID:  <75F63D9C-0EF2-40EB-BDC6-A2ADFD3C3A89@freebsd.org>
In-Reply-To: <010001688c2cfc3e-e319d851-8b9e-4468-8bd1-f93331f35116-000000@email.amazonses.com>
References:  <201901262136.x0QLaAJv095518@pdx.rh.CN85.dnsmgr.net> <010001688c2cfc3e-e319d851-8b9e-4468-8bd1-f93331f35116-000000@email.amazonses.com>

next in thread | previous in thread | raw e-mail | index | archive | help


> On 26 Jan 2019, at 1:59 PM, Colin Percival <cperciva@tarsnap.com> wrote:
>=20
> On 1/26/19 1:36 PM, Rodney W. Grimes wrote:
>>> Author: se
>>> Date: Sat Jan 26 21:30:26 2019
>>> New Revision: 343480
>>> URL: https://svnweb.freebsd.org/changeset/base/343480
>>>=20
>>> Log:
>>>  Silence Clang Scan warning about potentially unsafe use of strcpy.
>>>=20
>>>  While this is a false positive, the use of strdup() simplifies the code=
.
>>=20
>> Though that might be true, it also has to recalculate the
>> length of the string which was already known by slen.
>>=20
>> I am not sure how often this code is called,
>> but that is wasted cycles in a library.
>=20
> The length of the string was already being recalculated, by strcpy.
>=20
> It seems to me that this could be written as
>=20
>    temp =3D malloc(slen + 1);
>    if (temp =3D=3D NULL) /* could not allocate memory */
>        return (-1);
>    memcpy(temp, source, slen + 1);
>=20
> which avoids both recalculating the string length and using strcpy?
>=20

Thanks for fixing that.
=E2=80=94=20
Cheers,
Devin



>>> Modified: head/lib/libfigpar/string_m.c
>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
>>> --- head/lib/libfigpar/string_m.c    Sat Jan 26 20:43:28 2019    (r34347=
9)
>>> +++ head/lib/libfigpar/string_m.c    Sat Jan 26 21:30:26 2019    (r34348=
0)
>>> @@ -119,10 +119,9 @@ replaceall(char *source, const char *find, const ch=
ar=20
>>>=20
>>>    /* If replace is longer than find, we'll need to create a temp copy *=
/
>>>    if (rlen > flen) {
>>> -        temp =3D malloc(slen + 1);
>>> -        if (errno !=3D 0) /* could not allocate memory */
>>> +        temp =3D strdup(source);
>>> +        if (temp =3D=3D NULL) /* could not allocate memory */
>>>            return (-1);
>>> -        strcpy(temp, source);
>>>    } else
>>>        temp =3D source;




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?75F63D9C-0EF2-40EB-BDC6-A2ADFD3C3A89>