X-Git-Url: https://git.saurik.com/apple/libc.git/blobdiff_plain/34e8f8296870d0e8695f90e1a54240a589d41312..4c63d2152434d7a24cd627ef559f93b096274076:/secure/strncat_chk.c diff --git a/secure/strncat_chk.c b/secure/strncat_chk.c index fb0cff5..184023c 100644 --- a/secure/strncat_chk.c +++ b/secure/strncat_chk.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007 Apple Inc. All rights reserved. + * Copyright (c) 2007-2013 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * @@ -23,36 +23,24 @@ #include #include - -extern void __chk_fail (void) __attribute__((__noreturn__)); +#include "secure.h" char * -__strncat_chk (char *restrict dest, const char *restrict src, - size_t len, size_t dstlen) +__strncat_chk (char *restrict dest, const char *restrict append, + size_t len, size_t dstlen) { - char *s1 = dest; - const char *s2 = src; + size_t len1 = strlen(dest); + size_t len2 = strnlen(append, len); - /* Advance to the end. */ - while (*s1 != 0) - { - if (__builtin_expect (dstlen-- == 0, 0)) - __chk_fail (); - s1++; - } + if (__builtin_expect (dstlen < len1 + len2 + 1, 0)) + __chk_fail_overflow (); - /* Append the string. */ - while (len > 0) - { - if (__builtin_expect (dstlen-- == 0, 0)) - __chk_fail (); - if ((*s1 = *s2++) == 0) - break; - s1++; - len--; - } - *s1 = 0; + if (__builtin_expect (__chk_assert_no_overlap != 0, 1)) + __chk_overlap(dest, len1 + len2 + 1, append, len2 + 1); - return dest; + /* memmove() all but the NUL, since it might not actually be NUL */ + memcpy(dest + len1, append, len2); + dest[len1 + len2] = '\0'; + return dest; }