From: lurchi Date: Thu, 27 Jun 2019 09:37:34 +0000 (+0200) Subject: make GNUNET_strlcpy more flexible by using strnlen instead of strlen X-Git-Tag: v0.11.6~67 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=62fccf2d984f8e0d537fe919d095f3776bd3369c;p=oweals%2Fgnunet.git make GNUNET_strlcpy more flexible by using strnlen instead of strlen --- diff --git a/src/exit/gnunet-helper-exit-windows.c b/src/exit/gnunet-helper-exit-windows.c index 85a06c539..2e4b5f4a2 100644 --- a/src/exit/gnunet-helper-exit-windows.c +++ b/src/exit/gnunet-helper-exit-windows.c @@ -252,15 +252,12 @@ typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); /** - * Like strlcpy but portable. The given string @a src is copied in full length - * (until its null byte). The destination buffer is guaranteed to be - * null-terminated. + * Like strlcpy but portable. The given string @a src is copied until its null + * byte or until @a n - 1 bytes have been read. The destination buffer is + * guaranteed to be null-terminated. * - * to a destination buffer - * and ensures that the destination string is null-terminated. - * - * @param dst destination of the copy - * @param src source of the copy, must be null-terminated + * @param dst destination of the copy (must be @a n bytes long) + * @param src source of the copy (at most @a n - 1 bytes will be read) * @param n the length of the string to copy, including its terminating null * byte * @return the length of the string that was copied, excluding the terminating @@ -273,11 +270,10 @@ GNUNET_strlcpy(char *dst, const char *src, size_t n) size_t slen; GNUNET_assert (0 != n); - ret = strlen (src); - slen = GNUNET_MIN (ret, n - 1); + slen = strnlen (src, n - 1); memcpy (dst, src, slen); dst[slen] = '\0'; - return ret; + return slen; } diff --git a/src/util/strings.c b/src/util/strings.c index ae0544296..d83c36ef8 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -203,15 +203,12 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size) /** - * Like strlcpy but portable. The given string @a src is copied in full length - * (until its null byte). The destination buffer is guaranteed to be - * null-terminated. + * Like strlcpy but portable. The given string @a src is copied until its null + * byte or until @a n - 1 bytes have been read. The destination buffer is + * guaranteed to be null-terminated. * - * to a destination buffer - * and ensures that the destination string is null-terminated. - * - * @param dst destination of the copy - * @param src source of the copy, must be null-terminated + * @param dst destination of the copy (must be @a n bytes long) + * @param src source of the copy (at most @a n - 1 bytes will be read) * @param n the length of the string to copy, including its terminating null * byte * @return the length of the string that was copied, excluding the terminating @@ -224,11 +221,10 @@ GNUNET_strlcpy(char *dst, const char *src, size_t n) size_t slen; GNUNET_assert (0 != n); - ret = strlen (src); - slen = GNUNET_MIN (ret, n - 1); + slen = strnlen (src, n - 1); memcpy (dst, src, slen); dst[slen] = '\0'; - return ret; + return slen; } diff --git a/src/vpn/gnunet-helper-vpn-windows.c b/src/vpn/gnunet-helper-vpn-windows.c index ea4d30347..ab72d71aa 100644 --- a/src/vpn/gnunet-helper-vpn-windows.c +++ b/src/vpn/gnunet-helper-vpn-windows.c @@ -252,15 +252,12 @@ typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); /** - * Like strlcpy but portable. The given string @a src is copied in full length - * (until its null byte). The destination buffer is guaranteed to be - * null-terminated. + * Like strlcpy but portable. The given string @a src is copied until its null + * byte or until @a n - 1 bytes have been read. The destination buffer is + * guaranteed to be null-terminated. * - * to a destination buffer - * and ensures that the destination string is null-terminated. - * - * @param dst destination of the copy - * @param src source of the copy, must be null-terminated + * @param dst destination of the copy (must be @a n bytes long) + * @param src source of the copy (at most @a n - 1 bytes will be read) * @param n the length of the string to copy, including its terminating null * byte * @return the length of the string that was copied, excluding the terminating @@ -273,11 +270,10 @@ GNUNET_strlcpy(char *dst, const char *src, size_t n) size_t slen; GNUNET_assert (0 != n); - ret = strlen (src); - slen = GNUNET_MIN (ret, n - 1); + slen = strnlen (src, n - 1); memcpy (dst, src, slen); dst[slen] = '\0'; - return ret; + return slen; }