X-Git-Url: https://git.saurik.com/apple/libc.git/blobdiff_plain/224c70764cab4e0e39a26aaf3ad3016552f62f55..6465356a983ac139f81d3b7913cdb548477c346c:/secure/strcat_chk.c diff --git a/secure/strcat_chk.c b/secure/strcat_chk.c index e0ace2b..c6be25c 100644 --- a/secure/strcat_chk.c +++ b/secure/strcat_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@ * @@ -24,28 +24,21 @@ #include #include #include - -extern void __chk_fail (void) __attribute__((__noreturn__)); +#include "secure.h" char * -__strcat_chk (char *__restrict s, const char *__restrict append, - size_t slen) +__strcat_chk (char *__restrict dest, const char *__restrict append, + size_t dstlen) { - char *save = s; - - /* Advance to the end. */ - for (; *s; ++s) - if (__builtin_expect (slen-- == 0, 0)) - __chk_fail (); - - do - { - /* Append the string. Make sure we check before writing. */ - if (__builtin_expect (slen-- == 0, 0)) - __chk_fail (); + size_t len1 = strlen(dest); + size_t len2 = strlen(append); - } while (*s++ = *append++); + if (__builtin_expect (dstlen < len1 + len2 + 1, 0)) + __chk_fail_overflow (); - return save; + if (__builtin_expect (__chk_assert_no_overlap, 1)) + __chk_overlap(dest, len1 + len2 + 1, append, len2 + 1); + memcpy(dest + len1, append, len2 + 1); + return dest; }