From 0d0325ed4b1052f32069eb3a51f7e16485a73568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20B=C3=ADlka?= Date: Tue, 25 Aug 2015 12:23:24 +0200 Subject: Fix strcpy_chk and stpcpy_chk performance. Hi, as I wrote in previous patches a performance of checked strcpy and stpcpy is terrible as these don't use sse2 and are around four times slower that strcpy and stpcpy now. As this bug shows that these functions are not performance sensitive I decided just to improve generic implementation instead for easier maintainance. * debug/strcpy_chk.c: Improve performance. * debug/stpcpy_chk.c: Likewise. * sysdeps/x86_64/strcpy_chk.S: Remove. * sysdeps/x86_64/stpcpy_chk.S: Remove. --- debug/stpcpy_chk.c | 22 ++++++---------------- debug/strcpy_chk.c | 44 +++++--------------------------------------- 2 files changed, 11 insertions(+), 55 deletions(-) (limited to 'debug') diff --git a/debug/stpcpy_chk.c b/debug/stpcpy_chk.c index 91c50316ec..d9e4563cff 100644 --- a/debug/stpcpy_chk.c +++ b/debug/stpcpy_chk.c @@ -24,21 +24,11 @@ /* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */ char * -__stpcpy_chk (dest, src, destlen) - char *dest; - const char *src; - size_t destlen; +__stpcpy_chk (char *dest, const char *src, size_t destlen) { - char *d = dest; - const char *s = src; - - do - { - if (__glibc_unlikely (destlen-- == 0)) - __chk_fail (); - *d++ = *s; - } - while (*s++ != '\0'); - - return d - 1; + size_t len = strlen (src); + if (len >= destlen) + __chk_fail (); + + return memcpy (dest, src, len + 1) + len; } diff --git a/debug/strcpy_chk.c b/debug/strcpy_chk.c index 91bf0dd776..7cacbfe25f 100644 --- a/debug/strcpy_chk.c +++ b/debug/strcpy_chk.c @@ -23,45 +23,11 @@ /* Copy SRC to DEST with checking of destination buffer overflow. */ char * -__strcpy_chk (dest, src, destlen) - char *dest; - const char *src; - size_t destlen; +__strcpy_chk (char *dest, const char *src, size_t destlen) { - char c; - char *s = (char *) src; - const ptrdiff_t off = dest - s; + size_t len = strlen (src); + if (len >= destlen) + __chk_fail (); - while (__builtin_expect (destlen >= 4, 0)) - { - c = s[0]; - s[off] = c; - if (c == '\0') - return dest; - c = s[1]; - s[off + 1] = c; - if (c == '\0') - return dest; - c = s[2]; - s[off + 2] = c; - if (c == '\0') - return dest; - c = s[3]; - s[off + 3] = c; - if (c == '\0') - return dest; - destlen -= 4; - s += 4; - } - - do - { - if (__glibc_unlikely (destlen-- == 0)) - __chk_fail (); - c = *s; - *(s++ + off) = c; - } - while (c != '\0'); - - return dest; + return memcpy (dest, src, len + 1); } -- cgit v1.2.3