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/strlcat.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/strlcat.c')
-rw-r--r-- | main/strlcat.c | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/main/strlcat.c b/main/strlcat.c index 242819ac6f..a885ac304c 100644 --- a/main/strlcat.c +++ b/main/strlcat.c @@ -69,29 +69,34 @@ PHPAPI size_t php_strlcat(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 *d = dst; + const char *s = src; + size_t n = siz; size_t dlen; /* Find the end of dst and adjust bytes left but don't go past end */ - while (*d != '\0' && n-- != 0) - d++; - dlen = d - dst; + while (n-- != 0 && *dst != '\0') + dst++; + dlen = (uintptr_t)dst - (uintptr_t)d; n = siz - dlen; - if (n == 0) - return(dlen + strlen(s)); - while (*s != '\0') { - if (n != 1) { - *d++ = *s; + if (n-- == 0) + return(dlen + strlen(src)); + while (*src != '\0') { + if (n != 0) { + *dst++ = *src; n--; } - s++; + src++; } - *d = '\0'; + *dst = '\0'; - return(dlen + (s - src)); /* 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(dlen + ((uintptr_t)src - (uintptr_t)s)); } #endif /* !HAVE_STRLCAT */ |