memmem-oob regression test, update string tests with align/tail
authorSzabolcs Nagy <nsz@port70.net>
Wed, 9 Apr 2014 13:43:45 +0000 (15:43 +0200)
committerSzabolcs Nagy <nsz@port70.net>
Wed, 9 Apr 2014 13:43:45 +0000 (15:43 +0200)
src/functional/string_memmem.c
src/functional/string_strchr.c
src/regression/memmem-oob.c [new file with mode: 0644]

index c01b971..005045b 100644 (file)
@@ -4,11 +4,12 @@
 #include <string.h>
 #include "test.h"
 
-#define N(s, sub) { \
-       char *p = s; \
-       char *q = memmem(p, strlen(p), sub, strlen(sub)); \
+#define N(s, tail, sub) { \
+       char *p = s tail; \
+       char *q = memmem(p, strlen(s), sub, strlen(sub)); \
        if (q) \
-               t_error("memmem(%s,%s) returned str+%d, wanted 0\n", #s, #sub, q-p); \
+               t_error("memmem("#s" "#tail", %d, "#sub", %d) returned str+%d, wanted 0\n",\
+                       strlen(s), strlen(sub), q-p); \
 }
 
 #define T(s, sub, n) { \
 
 int main(void)
 {
-       N("", "a")
-       N("a", "aa")
-       N("a", "b")
-       N("aa", "ab")
-       N("aa", "aaa")
-       N("abba", "aba")
-       N("abc abc", "abcd")
-       N("0-1-2-3-4-5-6-7-8-9", "-3-4-56-7-8-")
-       N("0-1-2-3-4-5-6-7-8-9", "-3-4-5+6-7-8-")
-       N("_ _ _\xff_ _ _", "_\x7f_")
-       N("_ _ _\x7f_ _ _", "_\xff_")
+       N("","a", "a")
+       N("a","a", "aa")
+       N("a","b", "b")
+       N("aa","b", "ab")
+       N("aa","a", "aaa")
+       N("aba","b", "bab")
+       N("abba","b", "bab")
+       N("abba","ba", "aba")
+       N("abc abc","d", "abcd")
+       N("0-1-2-3-4-5-6-7-8-9","", "-3-4-56-7-8-")
+       N("0-1-2-3-4-5-6-7-8-9","", "-3-4-5+6-7-8-")
+       N("_ _ _\xff_ _ _","\x7f_", "_\x7f_")
+       N("_ _ _\x7f_ _ _","\xff_", "_\xff_")
 
        T("", "", 0)
        T("abcd", "", 0)
index e2df24a..883db91 100644 (file)
@@ -1,20 +1,38 @@
 #include <string.h>
 #include "test.h"
 
+static char buf[512];
+
+static void *aligned(void *p)
+{
+       return (void*)(((uintptr_t)p + 63) & -64U);
+}
+
+static void *aligncpy(void *p, size_t len, size_t a)
+{
+       return memcpy(aligned(buf)+a, p, len);
+}
+
 #define N(s, c) { \
-       char *p = s; \
-       char *q = strchr(p, c); \
-       if (q) \
-               t_error("strchr(%s,%s) returned str+%d, wanted 0\n", #s, #c, q-p); \
+       int align; \
+       for (align=0; align<8; align++) { \
+               char *p = aligncpy(s, sizeof s, align); \
+               char *q = strchr(p, c); \
+               if (q) \
+                       t_error("strchr(%s,%s) with align=%d returned str+%d, wanted 0\n", #s, #c, align, q-p); \
+       } \
 }
 
 #define T(s, c, n) { \
-       char *p = s; \
-       char *q = strchr(p, c); \
-       if (q == 0) \
-               t_error("strchr(%s,%s) returned 0, wanted str+%d\n", #s, #c, n); \
-       else if (q - p != n) \
-               t_error("strchr(%s,%s) returned str+%d, wanted str+%d\n", #s, #c, q-p, n); \
+       int align; \
+       for (align=0; align<8; align++) { \
+               char *p = aligncpy(s, sizeof s, align); \
+               char *q = strchr(p, c); \
+               if (q == 0) \
+                       t_error("strchr(%s,%s) with align=%d returned 0, wanted str+%d\n", #s, #c, align, n); \
+               else if (q - p != n) \
+                       t_error("strchr(%s,%s) with align=%d returned str+%d, wanted str+%d\n", #s, #c, align, q-p, n); \
+       } \
 }
 
 int main(void)
@@ -28,9 +46,11 @@ int main(void)
        for (i = 0; i < 256; i++)
                *((unsigned char*)s+i) = i+1;
 
-       N("", 'a')
-       N("a", 'b')
-       N("abc abc", 'x')
+       N("\0aaa", 'a')
+       N("a\0bb", 'b')
+       N("ab\0c", 'c')
+       N("abc\0d", 'd')
+       N("abc abc\0x", 'x')
        N(a, 128)
        N(a, 255)
 
@@ -38,12 +58,12 @@ int main(void)
        T("a", 'a', 0)
        T("a", 'a'+256, 0)
        T("a", 0, 1)
-       T("ab", 'b', 1)
-       T("aab", 'b', 2)
-       T("aaab", 'b', 3)
-       T("aaaab", 'b', 4)
-       T("aaaaab", 'b', 5)
-       T("aaaaaab", 'b', 6)
+       T("abb", 'b', 1)
+       T("aabb", 'b', 2)
+       T("aaabb", 'b', 3)
+       T("aaaabb", 'b', 4)
+       T("aaaaabb", 'b', 5)
+       T("aaaaaabb", 'b', 6)
        T("abc abc", 'c', 2)
        T(s, 1, 0)
        T(s, 2, 1)
diff --git a/src/regression/memmem-oob.c b/src/regression/memmem-oob.c
new file mode 100644 (file)
index 0000000..f3c57ad
--- /dev/null
@@ -0,0 +1,11 @@
+// memmem should not access oob data
+#include <string.h>
+#include "test.h"
+
+int main(void)
+{
+       char *p = memmem("abcde", 4, "cde", 3);
+       if (p)
+               t_error("memmem(abcde,4,cde,3) returned %s, want NULL\n", p);
+       return t_status;
+}