summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlurchi <lurchi@strangeplace.net>2019-06-27 11:37:34 +0200
committerlurchi <lurchi@strangeplace.net>2019-06-27 11:41:40 +0200
commit62fccf2d984f8e0d537fe919d095f3776bd3369c (patch)
tree8e66e89e32ab8b98debcb6eb87489f2a4f3f30aa
parentf54e8c82dc843aee294e61882d049b3870b458f0 (diff)
make GNUNET_strlcpy more flexible by using strnlen instead of strlen
-rw-r--r--src/exit/gnunet-helper-exit-windows.c18
-rw-r--r--src/util/strings.c18
-rw-r--r--src/vpn/gnunet-helper-vpn-windows.c18
3 files changed, 21 insertions, 33 deletions
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;
}