diff options
author | David Carlier <devnexen@gmail.com> | 2016-10-15 14:53:38 +0100 |
---|---|---|
committer | David Carlier <devnexen@gmail.com> | 2016-10-15 14:53:38 +0100 |
commit | 2464dbd5f3dda7ab69f9217d802d08af0334ec71 (patch) | |
tree | e8a6abc5848f686c0c5f116845c58ae679f6170a /main/strlcpy.c | |
parent | 2bd34885da3ccbab5b0007870b4f1e2a93052702 (diff) | |
download | php-git-2464dbd5f3dda7ab69f9217d802d08af0334ec71.tar.gz |
import explicit_bzero + strlc* functions update
since 1999 algorithms have changed and register k/w
not necessary anymore.
Diffstat (limited to 'main/strlcpy.c')
-rw-r--r-- | main/strlcpy.c | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/main/strlcpy.c b/main/strlcpy.c index 60d3e9906d..09008664f6 100644 --- a/main/strlcpy.c +++ b/main/strlcpy.c @@ -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 */ |