summaryrefslogtreecommitdiff
path: root/main/strlcpy.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/strlcpy.c')
-rw-r--r--main/strlcpy.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/main/strlcpy.c b/main/strlcpy.c
index 8d7b2652ea..3e66140ef6 100644
--- a/main/strlcpy.c
+++ b/main/strlcpy.c
@@ -22,7 +22,7 @@
#ifndef HAVE_STRLCPY
-/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */
+/* $OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $ */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -52,7 +52,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $";
+static char *rcsid = "$OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
@@ -68,27 +68,31 @@ PHPAPI size_t php_strlcpy(dst, src, siz)
const char *src;
size_t siz;
{
- register char *d = dst;
- register const char *s = src;
- register size_t n = siz;
+ const char *s = src;
+ size_t n = siz;
/* Copy as many bytes as will fit */
- if (n != 0 && --n != 0) {
- do {
- if ((*d++ = *s++) == 0)
+ if (n != 0) {
+ while (--n != 0) {
+ if ((*dst++ = *src++) == 0)
break;
- } while (--n != 0);
+ }
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
- *d = '\0'; /* NUL-terminate dst */
- while (*s++)
+ *dst = '\0'; /* NUL-terminate dst */
+ while (*src++)
;
}
- return(s - src - 1); /* count does not include NUL */
+ /*
+ * Cast pointers to unsigned type before calculation, to avoid signed
+ * overflow when the string ends where the MSB has changed.
+ * Return value does not include NUL.
+ */
+ return((uintptr_t)src - (uintptr_t)s - 1);
}
#endif /* !HAVE_STRLCPY */