[neomutt-devel] strfcpy() improvement
Fabian Groffen
grobian at gentoo.org
Tue Oct 4 15:07:09 CEST 2016
On 04-10-2016 13:23:09 +0100, Richard Russon wrote:
> Next, Mutt created a macro strfcpy() based on the BSD function. It
> guarantees a length limit AND a NULL termination.
>
> #define strfcpy(DST,SRC,LEN) strncpy(DST,SRC,LEN), *(DST+(LEN)-1)=0
>
> Because of the way it works, it triggers a warning in Coverity (a static
> analysis tool). It fills DST (without NULL), then writes the NULL.
>
> Why have I told you all of this? Because I'm changing the macro.
> dotlock.c lib.h rfc822.c
> My testing shows it works correctly, but I may missed something.
>
> #define strfcpy(DST,SRC,LEN) do { if ((LEN) > 0) { *(DST+(LEN)-1)=0; strncpy(DST,SRC,(LEN)-1); } } while (0)
Why not:
#define strfcpy(DST,SRC,LEN) snprintf(DST,LEN,"%s",SRC)
It simplifies the macro considerably, not sure about the performance
effect though.
Fabian
--
Fabian Groffen
Gentoo on a different level
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <http://mailman.neomutt.org/mailman/private/neomutt-devel-neomutt.org/attachments/20161004/5817010d/attachment.sig>
More information about the neomutt-devel
mailing list