From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stephen Hemminger Subject: Re: [RFC PATCH] use strlcpy for string copies Date: Tue, 20 Feb 2018 15:01:53 -0800 Message-ID: <20180220150153.1ba35bba@xeon-e3> References: <20180220170727.220340-1-bruce.richardson@intel.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: dev@dpdk.org To: Bruce Richardson Return-path: Received: from mail-pg0-f47.google.com (mail-pg0-f47.google.com [74.125.83.47]) by dpdk.org (Postfix) with ESMTP id AFAFE5598 for ; Wed, 21 Feb 2018 00:02:03 +0100 (CET) Received: by mail-pg0-f47.google.com with SMTP id f6so7708828pgs.10 for ; Tue, 20 Feb 2018 15:02:03 -0800 (PST) In-Reply-To: <20180220170727.220340-1-bruce.richardson@intel.com> List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" On Tue, 20 Feb 2018 17:07:27 +0000 Bruce Richardson 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 > * 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 Looks good, especially not reinventing strlcpy and using libbsd. Reviewed-by: Stephen Hemminger