diff options
author | lurchi <lurchi@strangeplace.net> | 2019-06-27 11:37:34 +0200 |
---|---|---|
committer | lurchi <lurchi@strangeplace.net> | 2019-06-27 11:41:40 +0200 |
commit | 62fccf2d984f8e0d537fe919d095f3776bd3369c (patch) | |
tree | 8e66e89e32ab8b98debcb6eb87489f2a4f3f30aa /src/util | |
parent | f54e8c82dc843aee294e61882d049b3870b458f0 (diff) | |
download | gnunet-62fccf2d984f8e0d537fe919d095f3776bd3369c.tar.gz gnunet-62fccf2d984f8e0d537fe919d095f3776bd3369c.zip |
make GNUNET_strlcpy more flexible by using strnlen instead of strlen
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/strings.c | 18 |
1 files changed, 7 insertions, 11 deletions
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) | |||
203 | 203 | ||
204 | 204 | ||
205 | /** | 205 | /** |
206 | * Like strlcpy but portable. The given string @a src is copied in full length | 206 | * Like strlcpy but portable. The given string @a src is copied until its null |
207 | * (until its null byte). The destination buffer is guaranteed to be | 207 | * byte or until @a n - 1 bytes have been read. The destination buffer is |
208 | * null-terminated. | 208 | * guaranteed to be null-terminated. |
209 | * | 209 | * |
210 | * to a destination buffer | 210 | * @param dst destination of the copy (must be @a n bytes long) |
211 | * and ensures that the destination string is null-terminated. | 211 | * @param src source of the copy (at most @a n - 1 bytes will be read) |
212 | * | ||
213 | * @param dst destination of the copy | ||
214 | * @param src source of the copy, must be null-terminated | ||
215 | * @param n the length of the string to copy, including its terminating null | 212 | * @param n the length of the string to copy, including its terminating null |
216 | * byte | 213 | * byte |
217 | * @return the length of the string that was copied, excluding the terminating | 214 | * @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) | |||
224 | size_t slen; | 221 | size_t slen; |
225 | 222 | ||
226 | GNUNET_assert (0 != n); | 223 | GNUNET_assert (0 != n); |
227 | ret = strlen (src); | 224 | slen = strnlen (src, n - 1); |
228 | slen = GNUNET_MIN (ret, n - 1); | ||
229 | memcpy (dst, src, slen); | 225 | memcpy (dst, src, slen); |
230 | dst[slen] = '\0'; | 226 | dst[slen] = '\0'; |
231 | return ret; | 227 | return slen; |
232 | } | 228 | } |
233 | 229 | ||
234 | 230 | ||