[neomutt-devel] strfcpy() improvement

Darshit Shah darnir at gmail.com
Wed Oct 5 12:58:02 CEST 2016


* Richard Russon <rich at flatcap.org> [161005 01:28]:
>> In fact, I would suggest turning it into a function
>
>I didn't actually consider that.
>I was just happy to eliminate 100 false positives from coverity :-)
>
>> Most modern compilers are smart enough to inline such a trivial function.
>
>True.  Not that anything in NeoMutt is *that* time-sensitive.
>
>> The more macros we get rid of, the cleaner and safer the code is.
>
>Agreed.  Plus the function could be quite simple.
>
>The original macro ran strndup which returns a pointer to 'dest'.
>There are 439 uses of strfcpy and none of them use the return value.
>
>However, a quick bit of research shows that using a function this way
>isn't considered wise, because the caller can't tell if the string has
>been truncated.
>
However, when the caller passes a LEN parameter, it expects the string 
to be truncated.

char *strfcpy(char *dest, const char *src, size_t n) {
  char *dest0 = dest;
  while (--len > 0 && *src != '\0') {
	*dest++ = *src++;
  }
  *dest = '\0'
  return dest0;
}

This is a simple implementation of how a strfcpy function would be 
written.

>I'm open to suggestions (and code)
>
>Rich



-- 
Thanking You,
Darshit Shah
PGP Fingerprint: 7845 120B 07CB D8D6 ECE5 FF2B 2A17 43ED A91A 35B6
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 866 bytes
Desc: not available
URL: <http://mailman.neomutt.org/mailman/private/neomutt-devel-neomutt.org/attachments/20161005/1970a1e5/attachment.sig>


More information about the neomutt-devel mailing list