fix aliasing-based undefined behavior in string functions
authorRich Felker <dalias@aerifal.cx>
Wed, 26 Sep 2018 18:39:10 +0000 (14:39 -0400)
committerRich Felker <dalias@aerifal.cx>
Wed, 26 Sep 2018 18:39:10 +0000 (14:39 -0400)
use the GNU C may_alias attribute if available, and fallback to naive
byte-by-byte loops if __GNUC__ is not defined.

this patch has been written to minimize changes so that history
remains reviewable; it does not attempt to bring the affected code
into a more consistent or elegant form.

src/string/memccpy.c
src/string/memchr.c
src/string/memmove.c
src/string/stpcpy.c
src/string/stpncpy.c
src/string/strchrnul.c
src/string/strlcpy.c
src/string/strlen.c

index 7c233d5..f515581 100644 (file)
@@ -11,19 +11,22 @@ void *memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
 {
        unsigned char *d = dest;
        const unsigned char *s = src;
-       size_t *wd, k;
-       const size_t *ws;
 
        c = (unsigned char)c;
+#ifdef __GNUC__
+       typedef size_t __attribute__((__may_alias__)) word;
+       word *wd;
+       const word *ws;
        if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) {
                for (; ((uintptr_t)s & ALIGN) && n && (*d=*s)!=c; n--, s++, d++);
                if ((uintptr_t)s & ALIGN) goto tail;
-               k = ONES * c;
+               size_t k = ONES * c;
                wd=(void *)d; ws=(const void *)s;
                for (; n>=sizeof(size_t) && !HASZERO(*ws^k);
                       n-=sizeof(size_t), ws++, wd++) *wd = *ws;
                d=(void *)wd; s=(const void *)ws;
        }
+#endif
        for (; n && (*d=*s)!=c; n--, s++, d++);
 tail:
        if (*s==c) return d+1;
index 4daff7b..65f0d78 100644 (file)
@@ -12,12 +12,16 @@ void *memchr(const void *src, int c, size_t n)
 {
        const unsigned char *s = src;
        c = (unsigned char)c;
+#ifdef __GNUC__
        for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--);
        if (n && *s != c) {
-               const size_t *w;
+               typedef size_t __attribute__((__may_alias__)) word;
+               const word *w;
                size_t k = ONES * c;
                for (w = (const void *)s; n>=SS && !HASZERO(*w^k); w++, n-=SS);
-               for (s = (const void *)w; n && *s != c; s++, n--);
+               s = (const void *)w;
        }
+#endif
+       for (; n && *s != c; s++, n--);
        return n ? (void *)s : 0;
 }
index f225bb3..5dc9cdb 100644 (file)
@@ -1,8 +1,10 @@
 #include <string.h>
 #include <stdint.h>
 
-#define WT size_t
+#ifdef __GNUC__
+typedef __attribute__((__may_alias__)) size_t WT;
 #define WS (sizeof(WT))
+#endif
 
 void *memmove(void *dest, const void *src, size_t n)
 {
@@ -13,6 +15,7 @@ void *memmove(void *dest, const void *src, size_t n)
        if ((uintptr_t)s-(uintptr_t)d-n <= -2*n) return memcpy(d, s, n);
 
        if (d<s) {
+#ifdef __GNUC__
                if ((uintptr_t)s % WS == (uintptr_t)d % WS) {
                        while ((uintptr_t)d % WS) {
                                if (!n--) return dest;
@@ -20,8 +23,10 @@ void *memmove(void *dest, const void *src, size_t n)
                        }
                        for (; n>=WS; n-=WS, d+=WS, s+=WS) *(WT *)d = *(WT *)s;
                }
+#endif
                for (; n; n--) *d++ = *s++;
        } else {
+#ifdef __GNUC__
                if ((uintptr_t)s % WS == (uintptr_t)d % WS) {
                        while ((uintptr_t)(d+n) % WS) {
                                if (!n--) return dest;
@@ -29,6 +34,7 @@ void *memmove(void *dest, const void *src, size_t n)
                        }
                        while (n>=WS) n-=WS, *(WT *)(d+n) = *(WT *)(s+n);
                }
+#endif
                while (n) n--, d[n] = s[n];
        }
 
index 54cf9ca..4db46a9 100644 (file)
@@ -9,9 +9,10 @@
 
 char *__stpcpy(char *restrict d, const char *restrict s)
 {
-       size_t *wd;
-       const size_t *ws;
-
+#ifdef __GNUC__
+       typedef size_t __attribute__((__may_alias__)) word;
+       word *wd;
+       const word *ws;
        if ((uintptr_t)s % ALIGN == (uintptr_t)d % ALIGN) {
                for (; (uintptr_t)s % ALIGN; s++, d++)
                        if (!(*d=*s)) return d;
@@ -19,6 +20,7 @@ char *__stpcpy(char *restrict d, const char *restrict s)
                for (; !HASZERO(*ws); *wd++ = *ws++);
                d=(void *)wd; s=(const void *)ws;
        }
+#endif
        for (; (*d=*s); s++, d++);
 
        return d;
index d6d92ff..f57fa6b 100644 (file)
@@ -9,9 +9,10 @@
 
 char *__stpncpy(char *restrict d, const char *restrict s, size_t n)
 {
-       size_t *wd;
-       const size_t *ws;
-
+#ifdef __GNUC__
+       typedef size_t __attribute__((__may_alias__)) word;
+       word *wd;
+       const word *ws;
        if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) {
                for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++);
                if (!n || !*s) goto tail;
@@ -20,6 +21,7 @@ char *__stpncpy(char *restrict d, const char *restrict s, size_t n)
                       n-=sizeof(size_t), ws++, wd++) *wd = *ws;
                d=(void *)wd; s=(const void *)ws;
        }
+#endif
        for (; n && (*d=*s); n--, s++, d++);
 tail:
        memset(d, 0, n);
index f2b9ae1..39e2635 100644 (file)
@@ -9,16 +9,19 @@
 
 char *__strchrnul(const char *s, int c)
 {
-       size_t *w, k;
-
        c = (unsigned char)c;
        if (!c) return (char *)s + strlen(s);
 
+#ifdef __GNUC__
+       typedef size_t __attribute__((__may_alias__)) word;
+       const word *w;
        for (; (uintptr_t)s % ALIGN; s++)
                if (!*s || *(unsigned char *)s == c) return (char *)s;
-       k = ONES * c;
+       size_t k = ONES * c;
        for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++);
-       for (s = (void *)w; *s && *(unsigned char *)s != c; s++);
+       s = (void *)w;
+#endif
+       for (; *s && *(unsigned char *)s != c; s++);
        return (char *)s;
 }
 
index dcb22f6..ffa0b0b 100644 (file)
@@ -12,9 +12,11 @@ size_t strlcpy(char *d, const char *s, size_t n)
 {
        char *d0 = d;
        size_t *wd;
-       const size_t *ws;
 
        if (!n--) goto finish;
+#ifdef __GNUC__
+       typedef size_t __attribute__((__may_alias__)) word;
+       const word *ws;
        if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) {
                for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++);
                if (n && *s) {
@@ -24,6 +26,7 @@ size_t strlcpy(char *d, const char *s, size_t n)
                        d=(void *)wd; s=(const void *)ws;
                }
        }
+#endif
        for (; n && (*d=*s); n--, s++, d++);
        *d = 0;
 finish:
index 929ddcb..309990f 100644 (file)
 size_t strlen(const char *s)
 {
        const char *a = s;
-       const size_t *w;
+#ifdef __GNUC__
+       typedef size_t __attribute__((__may_alias__)) word;
+       const word *w;
        for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a;
        for (w = (const void *)s; !HASZERO(*w); w++);
-       for (s = (const void *)w; *s; s++);
+       s = (const void *)w;
+#endif
+       for (; *s; s++);
        return s-a;
 }