[dpdk-dev] [RFC PATCH] use strlcpy for string copies

Stephen Hemminger stephen at networkplumber.org
Wed Feb 21 00:01:53 CET 2018


On Tue, 20 Feb 2018 17:07:27 +0000
Bruce Richardson <bruce.richardson at intel.com> wrote:

> Following on from the number of patches needing to be done for strncpy
> issues highlighted by coverity...
> 
> The strncpy function is error prone for doing "safe" string copies, so
> we generally try to use "snprintf" instead in the code. The function
> "strlcpy" is a better alternative, though, since it better conveys the
> intention of the programmer, and doesn't suffer from the non-null
> terminating behaviour of it's n'ed brethern.
> 
> The downside of this function is that it is not available by default
> on linux, though standard in the BSD's. It is available on most
> distros by installing "libbsd" package.
> 
> This RFC therefore provides the following in rte_string_fns.h to ensure
> that strlcpy is available there:
> * for BSD, include string.h as normal
> * if RTE_USE_LIBBSD is set, include <bsd/string.h>
> * if not set, fallback to snprintf for strlcpy
> 
> Using make build system, the RTE_USE_LIBBSD is a hard-coded value to "n",
> but when using meson, it's automatically set based on what is available
> on the platform.
> 
> Instances of snprintf using "%s" alone as a string format are replaced
> via coccinelle script with the new strlcpy function. Instances of
> strncpy should be replaced too, but requires manual checking as to
> whether the NULL termination is manually done afterward or not.
> 
> Signed-off-by: Bruce Richardson <bruce.richardson at intel.com>

Looks good, especially not reinventing strlcpy and using libbsd.

Reviewed-by: Stephen Hemminger <stephen at networkplumber.org>


More information about the dev mailing list