From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.warmcat.com (mail.warmcat.com [163.172.24.82]) by dpdk.org (Postfix) with ESMTP id 9B998CF66 for ; Fri, 18 May 2018 14:16:06 +0200 (CEST) From: Andy Green To: stable@dpdk.org Date: Fri, 18 May 2018 20:16:01 +0800 Message-ID: <152664576179.32615.2417327867032729623.stgit@localhost.localdomain> In-Reply-To: <152664553026.32615.51601026908782839.stgit@localhost.localdomain> References: <152664553026.32615.51601026908782839.stgit@localhost.localdomain> User-Agent: StGit/unknown-version Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Subject: [dpdk-stable] [PATCH 1/4] eal: support strlcpy function X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 18 May 2018 12:16:06 -0000 From: Bruce Richardson 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, 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 patch 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. Signed-off-by: Bruce Richardson Reviewed-by: Stephen Hemminger --- config/common_base | 1 + devtools/cocci/strlcpy.cocci | 8 ++++++ lib/librte_eal/common/include/rte_string_fns.h | 31 ++++++++++++++++++++++++ mk/rte.app.mk | 3 ++ 4 files changed, 43 insertions(+) create mode 100644 devtools/cocci/strlcpy.cocci diff --git a/config/common_base b/config/common_base index 4bff83af7..75136a2e4 100644 --- a/config/common_base +++ b/config/common_base @@ -96,6 +96,7 @@ CONFIG_RTE_EAL_ALWAYS_PANIC_ON_ERROR=n CONFIG_RTE_EAL_IGB_UIO=n CONFIG_RTE_EAL_VFIO=n CONFIG_RTE_MALLOC_DEBUG=n +CONFIG_RTE_USE_LIBBSD=n # Default driver path (or "" to disable) CONFIG_RTE_EAL_PMD_PATH="" diff --git a/devtools/cocci/strlcpy.cocci b/devtools/cocci/strlcpy.cocci new file mode 100644 index 000000000..335e27128 --- /dev/null +++ b/devtools/cocci/strlcpy.cocci @@ -0,0 +1,8 @@ +@use_strlcpy@ +identifier src, dst; +expression size; +@@ +( +- snprintf(dst, size, "%s", src) ++ strlcpy(dst, src, size) +) diff --git a/lib/librte_eal/common/include/rte_string_fns.h b/lib/librte_eal/common/include/rte_string_fns.h index cfca2f8df..7c0ab15a3 100644 --- a/lib/librte_eal/common/include/rte_string_fns.h +++ b/lib/librte_eal/common/include/rte_string_fns.h @@ -44,6 +44,8 @@ extern "C" { #endif +#include + /** * Takes string "string" parameter and splits it at character "delim" * up to maxtokens-1 times - to give "maxtokens" resulting tokens. Like @@ -74,6 +76,35 @@ int rte_strsplit(char *string, int stringlen, char **tokens, int maxtokens, char delim); +/** + * @internal + * DPDK-specific version of strlcpy for systems without + * libc or libbsd copies of the function + */ +static inline size_t +rte_strlcpy(char *dst, const char *src, size_t size) +{ + return snprintf(dst, size, "%s", src); +} + +/* pull in a strlcpy function */ +#ifdef RTE_EXEC_ENV_BSDAPP +#include +#ifndef __BSD_VISIBLE /* non-standard functions are hidden */ +#define strlcpy(dst, src, size) rte_strlcpy(dst, src, size) +#endif + + +#else /* non-BSD platforms */ +#ifdef RTE_USE_LIBBSD +#include + +#else /* no BSD header files, create own */ +#define strlcpy(dst, src, size) rte_strlcpy(dst, src, size) + +#endif /* RTE_USE_LIBBSD */ +#endif /* BSDAPP */ + #ifdef __cplusplus } #endif diff --git a/mk/rte.app.mk b/mk/rte.app.mk index f75f0e24d..feaa7ec8d 100644 --- a/mk/rte.app.mk +++ b/mk/rte.app.mk @@ -155,6 +155,9 @@ ifeq ($(CONFIG_RTE_BUILD_SHARED_LIB),n) # The static libraries do not know their dependencies. # So linking with static library requires explicit dependencies. _LDLIBS-$(CONFIG_RTE_LIBRTE_EAL) += -lrt +ifeq ($(CONFIG_RTE_EXEC_ENV_LINUXAPP)$(CONFIG_RTE_USE_LIBBSD),yy) +_LDLIBS-$(CONFIG_RTE_LIBRTE_EAL) += -lbsd +endif _LDLIBS-$(CONFIG_RTE_LIBRTE_SCHED) += -lm _LDLIBS-$(CONFIG_RTE_LIBRTE_SCHED) += -lrt _LDLIBS-$(CONFIG_RTE_LIBRTE_METER) += -lm