Re: Using the return value of strlcpy() and strlcat() - Mailing list pgsql-hackers

From Ashwin Agrawal
Subject Re: Using the return value of strlcpy() and strlcat()
Date
Msg-id CALfoeisXr118_wGphaC-tntav9t-nUU_DZ1uKH6cgrJhVQaGRA@mail.gmail.com
Whole thread Raw
In response to Re: Using the return value of strlcpy() and strlcat()  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Using the return value of strlcpy() and strlcat()
List pgsql-hackers

On Wed, Mar 13, 2019 at 9:51 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
ilmari@ilmari.org (Dagfinn Ilmari Mannsåker) writes:
> [ let's convert
> +             strlcpy(buf + buflen, name, NAMEDATALEN);
> +             buflen += strlen(buf + buflen);
> to
> +             buflen += strlcpy(buf + buflen, name, NAMEDATALEN);
> ]

I don't think that's a safe transformation: what strlcpy returns is
strlen(src), which might be different from what it was actually
able to fit into the destination.

Sure, they're equivalent if no truncation occurred; but if we were
100.00% sure of no truncation, we'd likely not bother with strlcpy.

So, if return value < length (3rd argument) we should be able to use the return value and avoid the strlen, else do the strlen ?

pgsql-hackers by date:

Previous
From: Amit Langote
Date:
Subject: Re: hyrax vs. RelationBuildPartitionDesc
Next
From: "Tsunakawa, Takayuki"
Date:
Subject: RE: Timeout parameters