aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJere Leppänen <jere.leppanen@nokia.com>2024-02-16 14:28:17 +0200
committerMatias Elo <matias.elo@nokia.com>2024-04-26 15:16:11 +0300
commitead9d5508ee6e96815942c902a10380e9880a545 (patch)
tree8303b25c14db293e34ec8f88763c2cb3fb237373
parent47e63c61dd17e6f11a8394aa50150563e551f0b3 (diff)
helper: add string copy function
Both strcpy() and strncpy() are error-prone, and checkpatch suggests using something else. Add odph_strcpy() function, which guarantees termination of the destination string. Signed-off-by: Jere Leppänen <jere.leppanen@nokia.com> Reviewed-by: Matias Elo <matias.elo@nokia.com>
-rw-r--r--helper/Makefile.am1
-rw-r--r--helper/include/odp/helper/odph_api.h1
-rw-r--r--helper/include/odp/helper/string.h62
3 files changed, 64 insertions, 0 deletions
diff --git a/helper/Makefile.am b/helper/Makefile.am
index f7685a221..bca50873e 100644
--- a/helper/Makefile.am
+++ b/helper/Makefile.am
@@ -30,6 +30,7 @@ helperinclude_HEADERS = \
include/odp/helper/odph_iplookuptable.h\
include/odp/helper/odph_lineartable.h\
include/odp/helper/sctp.h \
+ include/odp/helper/string.h\
include/odp/helper/strong_types.h\
include/odp/helper/tcp.h\
include/odp/helper/table.h\
diff --git a/helper/include/odp/helper/odph_api.h b/helper/include/odp/helper/odph_api.h
index 94d43a61b..d2ac2a55a 100644
--- a/helper/include/odp/helper/odph_api.h
+++ b/helper/include/odp/helper/odph_api.h
@@ -32,6 +32,7 @@ extern "C" {
#include <odp/helper/odph_lineartable.h>
#include <odp/helper/odph_iplookuptable.h>
#include <odp/helper/sctp.h>
+#include <odp/helper/string.h>
#include <odp/helper/strong_types.h>
#include <odp/helper/tcp.h>
#include <odp/helper/table.h>
diff --git a/helper/include/odp/helper/string.h b/helper/include/odp/helper/string.h
new file mode 100644
index 000000000..da2d61a6b
--- /dev/null
+++ b/helper/include/odp/helper/string.h
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 2024 Nokia
+ */
+
+/**
+ * @file
+ *
+ * ODP string helper
+ */
+
+#ifndef ODPH_STRING_H_
+#define ODPH_STRING_H_
+
+#include <odp/api/hints.h>
+#include <string.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @defgroup odph_string ODPH STRING
+ * String helper
+ *
+ * @{
+ */
+
+/**
+ * Copy a string
+ *
+ * Like strncpy(), but additionally ensures that the destination string is null
+ * terminated, unless sz is zero in which case returns dst without doing
+ * anything else.
+ *
+ * @param[out] dst Pointer to destination string.
+ * @param src Pointer to source string.
+ * @param sz Destination size.
+ * @return Pointer to destination string.
+ */
+#ifdef __cplusplus
+ODP_UNUSED static char *odph_strcpy(char *dst, const char *src, size_t sz)
+#else
+ODP_UNUSED static char *odph_strcpy(char *restrict dst, const char *restrict src, size_t sz)
+#endif
+{
+ if (!sz)
+ return dst;
+
+ strncpy(dst, src, sz - 1);
+ dst[sz - 1] = 0;
+ return dst;
+}
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif