[PATCH] libosmocore[master]: utils: add osmo_strncpy(), as drop-in replacement for strncpy()

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Fri Jan 13 13:41:28 UTC 2017


Review at  https://gerrit.osmocom.org/1598

utils: add osmo_strncpy(), as drop-in replacement for strncpy()

osmo_strlcpy() already exists, but it returns strlen(src). This does not match
the stock strncpy() signature, so is not a real drop-in replacement. Also, the
returned value and thus the iteration of src for strlen is usually not needed.

Change-Id: If8ba5361c1d0c90973a1447ce30c1198946a5265
---
M include/osmocom/core/utils.h
M src/utils.c
2 files changed, 32 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/98/1598/1

diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index 41bbc27..0d61901 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -87,5 +87,6 @@
 uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len);
 
 size_t osmo_strlcpy(char *dst, const char *src, size_t siz);
+char *osmo_strncpy(char *dst, const char *src, size_t siz);
 
 /*! @} */
diff --git a/src/utils.c b/src/utils.c
index 34b2bca..e2a5db2 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -348,6 +348,8 @@
  * Copy at most \a siz bytes from \a src to \a dst, ensuring that the result is
  * NUL terminated. The NUL character is included in \a siz, i.e. passing the
  * actual sizeof(*dst) is correct.
+ *
+ * \see osmo_strncpy()
  */
 size_t osmo_strlcpy(char *dst, const char *src, size_t siz)
 {
@@ -361,4 +363,33 @@
 	return ret;
 }
 
+/*! \brief Copy a C-string into a sized buffer safely.
+ *  \param[in] src source string
+ *  \param[out] dst destination string
+ *  \param[in] siz size of the \a dst buffer
+ *  \returns \a dst
+ *
+ * Copy at most \a siz bytes from \a src to \a dst, ensuring that the result is
+ * NUL terminated. The NUL character is included in \a siz, i.e. passing the
+ * actual sizeof(*dst) is correct.
+ *
+ * This function is the same as osmo_strlcpy(), but does not return the length
+ * of \a src. Like the usual strncpy(), this returns \a dst instead. This saves
+ * one iteration of \a src, so should be more optimal than osmo_strlcpy().
+ *
+ * If \a siz is zero or \a src is NULL, copy nothing and return \a dst.
+ *
+ * \see osmo_strlcpy
+ */
+char *osmo_strncpy(char *dst, const char *src, size_t siz)
+{
+	if (!siz || !dst)
+		return dst;
+	OSMO_ASSERT(src);
+	*dst = '\0';
+	/* strncat writes n chars *plus* a terminating '\0',
+	 * e.g. strncat(dst, src, 0) still writes a '\0'. */
+	return strncat(dst, src, siz - 1);
+}
+
 /*! @} */

-- 
To view, visit https://gerrit.osmocom.org/1598
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If8ba5361c1d0c90973a1447ce30c1198946a5265
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>


More information about the gerrit-log mailing list