From: Christian Brauner Date: Fri, 11 May 2018 11:02:41 +0000 (+0200) Subject: CODING_STYLE: add section about using strlcpy() X-Git-Tag: lxc-2.0.10~143 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8baf35702fedd98bc9844ea7543bdcab515c2ca2;p=thirdparty%2Flxc.git CODING_STYLE: add section about using strlcpy() Signed-off-by: Christian Brauner --- diff --git a/CODING_STYLE.md b/CODING_STYLE.md index 24c6d3603..784c5134e 100644 --- a/CODING_STYLE.md +++ b/CODING_STYLE.md @@ -662,3 +662,13 @@ rules to use them: #endif }; ``` + +#### Use `strlcpy()` instead of `strncpy()` + +When copying strings always use `strlcpy()` instead of `strncpy()`. The +advantage of `strlcpy()` is that it will always append a `\0` byte to the +string. + +Unless you have a valid reason to accept truncation you must check whether +truncation has occurred, treat it as an error, and handle the error +appropriately.