From: Andy Green <andy@warmcat.com>
To: stable@dpdk.org
Subject: [dpdk-stable] [PATCH 1/5] eal: support strlcpy function
Date: Fri, 18 May 2018 19:37:53 +0800 [thread overview]
Message-ID: <152664347386.25528.8508765648173406729.stgit@localhost.localdomain> (raw)
In-Reply-To: <152664333397.25528.18140247166845268675.stgit@localhost.localdomain>
From: Bruce Richardson <bruce.richardson@intel.com>
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 <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.
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
Reviewed-by: Stephen Hemminger <stephen@networkplumber.org>
---
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 e74febef4..4241e6f10 100644
--- a/config/common_base
+++ b/config/common_base
@@ -105,6 +105,7 @@ CONFIG_RTE_EAL_IGB_UIO=n
CONFIG_RTE_EAL_VFIO=n
CONFIG_RTE_MALLOC_DEBUG=n
CONFIG_RTE_EAL_NUMA_AWARE_HUGEPAGES=n
+CONFIG_RTE_USE_LIBBSD=n
#
# Recognize/ignore the AVX/AVX512 CPU flags for performance/power testing.
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 <stdio.h>
+
/**
* 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 <string.h>
+#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 <bsd/string.h>
+
+#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 6a6a7452e..98ab58de0 100644
--- a/mk/rte.app.mk
+++ b/mk/rte.app.mk
@@ -218,6 +218,9 @@ _LDLIBS-$(CONFIG_RTE_LIBRTE_EAL) += -lrt
ifeq ($(CONFIG_RTE_EXEC_ENV_LINUXAPP)$(CONFIG_RTE_EAL_NUMA_AWARE_HUGEPAGES),yy)
_LDLIBS-$(CONFIG_RTE_LIBRTE_EAL) += -lnuma
endif
+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_MEMBER) += -lm
next prev parent reply other threads:[~2018-05-18 11:38 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-30 15:23 [dpdk-stable] please help backporting some patches to stable release 18.02.2 luca.boccassi
2018-04-30 15:30 ` Maxime Coquelin
2018-04-30 15:44 ` Luca Boccassi
2018-04-30 16:06 ` Burakov, Anatoly
2018-04-30 16:33 ` Luca Boccassi
2018-05-02 6:04 ` Hyong Youb Kim
2018-05-02 10:43 ` Luca Boccassi
2018-05-02 8:27 ` Nélio Laranjeiro
2018-05-02 10:42 ` Luca Boccassi
2018-05-18 9:46 ` luca.boccassi
2018-05-18 10:11 ` Richardson, Bruce
2018-05-18 10:21 ` Luca Boccassi
2018-05-18 10:25 ` Andy Green
2018-05-18 10:33 ` Pattan, Reshma
2018-05-18 10:36 ` Andy Green
2018-05-18 11:37 ` [dpdk-stable] [PATCH 0/5] 18.02 Requested backports Andy Green
2018-05-18 11:37 ` Andy Green [this message]
2018-05-18 11:37 ` [dpdk-stable] [PATCH 2/5] bus/pci: fix size of driver name buffer Andy Green
2018-05-18 11:38 ` [dpdk-stable] [PATCH 3/5] net/qede: fix strncpy Andy Green
2018-05-18 11:38 ` [dpdk-stable] [PATCH 4/5] net/qede: replace strncpy by strlcpy Andy Green
2018-05-18 11:38 ` [dpdk-stable] [PATCH 5/5] net/sfc: make sure that stats name is nul-terminated Andy Green
2018-05-18 13:13 ` [dpdk-stable] [PATCH 0/5] 18.02 Requested backports Luca Boccassi
2018-05-25 17:23 ` [dpdk-stable] please help backporting some patches to stable release 18.02.2 luca.boccassi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=152664347386.25528.8508765648173406729.stgit@localhost.localdomain \
--to=andy@warmcat.com \
--cc=stable@dpdk.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).