This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
Neels Hofmeyr gerrit-no-reply at lists.osmocom.orgHello Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/1598 to look at the new patch set (#2). 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, 34 insertions(+), 0 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/98/1598/2 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..1daa7ea 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,35 @@ 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 dst is NULL or \a siz is zero, copy nothing and return \a dst. + * Otherwise, if \a src is NULL, merely write NUL to the start of \a dst. + * + * \see osmo_strlcpy + */ +char *osmo_strncpy(char *dst, const char *src, size_t siz) +{ + if (!dst || !siz) + return dst; + *dst = '\0'; + if (!src) + return dst; + /* 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: newpatchset Gerrit-Change-Id: If8ba5361c1d0c90973a1447ce30c1198946a5265 Gerrit-PatchSet: 2 Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de> Gerrit-Reviewer: Jenkins Builder