X-Git-Url: https://git.saurik.com/apple/libc.git/blobdiff_plain/15de9d6b4ab2de27ae24b13b7b6c4d55fffe4aef..974e388456677d82eb6d10d4fd72390641a5bdfe:/tests/strlcat.c diff --git a/tests/strlcat.c b/tests/strlcat.c index 6e649d1..af1713c 100644 --- a/tests/strlcat.c +++ b/tests/strlcat.c @@ -1,30 +1,29 @@ -#include #include #include #include +#include + static const char* qbf = "The quick brown fox jumps over the lazy dog"; static const char* lynx = "Lynx c.q. vos prikt bh: dag zwemjuf!"; -int -main(void) +T_DECL(strlcat, "strlcat(3)") { - test_start("strlcat"); void *ptr = mmap(NULL, PAGE_SIZE*2, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); - test_ptr_not("mmap", ptr, MAP_FAILED); + T_ASSERT_NE(ptr, MAP_FAILED, NULL); - test_errno("mprotect", mprotect(ptr+PAGE_SIZE, PAGE_SIZE, PROT_READ), 0); + T_ASSERT_POSIX_ZERO(mprotect(ptr+PAGE_SIZE, PAGE_SIZE, PROT_READ), NULL); - off_t offset = strlen(qbf)+strlen(lynx)+1; + size_t offset = strlen(qbf)+strlen(lynx)+1; char *dst = (ptr+PAGE_SIZE)-offset; strcpy(dst, qbf); size_t res = strlcat(dst, lynx, offset); - test_long("strlcat", res, offset-1); - test_long("memcmp", memcmp(dst, qbf, strlen(qbf)), 0); - test_long("memcmp", memcmp(dst+strlen(qbf), lynx, strlen(lynx)), 0); - test_long("null-term", dst[offset], 0); + T_ASSERT_EQ(res, offset-1, "strlcat"); + T_ASSERT_EQ(memcmp(dst, qbf, strlen(qbf)), 0, NULL); + T_ASSERT_EQ(memcmp(dst+strlen(qbf), lynx, strlen(lynx)), 0, NULL); + T_ASSERT_EQ(dst[offset], 0, "null-term"); memset(ptr, '\0', PAGE_SIZE); @@ -33,11 +32,11 @@ main(void) strcpy(dst, qbf); res = strlcat(dst, lynx, offset); - test_long("strlcat", res, strlen(qbf)+strlen(lynx)); - test_long("memcmp", memcmp(dst, qbf, strlen(qbf)), 0); - test_long("memcmp", memcmp(dst+strlen(qbf), lynx, offset-strlen(qbf)-1), 0); - test_long("overrun", *(char*)(ptr+PAGE_SIZE), 0); - test_long("null-term", dst[offset], 0); + T_ASSERT_EQ(res, strlen(qbf)+strlen(lynx), "strlcat"); + T_ASSERT_EQ(memcmp(dst, qbf, strlen(qbf)), 0, NULL); + T_ASSERT_EQ(memcmp(dst+strlen(qbf), lynx, offset-strlen(qbf)-1), 0, NULL); + T_ASSERT_EQ(*(char*)(ptr+PAGE_SIZE), 0, NULL); + T_ASSERT_EQ(dst[offset], 0, "null-term"); memset(ptr, '\0', PAGE_SIZE); @@ -46,11 +45,21 @@ main(void) strncpy(dst, qbf, offset); res = strlcat(dst, lynx, offset); - test_long("strlcat", res, offset+strlen(lynx)); - test_long("memcmp", memcmp(dst, qbf, offset), 0); - test_long("overrun", *(char*)(ptr+PAGE_SIZE), 0); - test_long("null-term", dst[offset], 0); - - test_stop(); - return EXIT_SUCCESS; -} \ No newline at end of file + T_ASSERT_EQ(res, offset+strlen(lynx), "strlcat"); + T_ASSERT_EQ(memcmp(dst, qbf, offset), 0, NULL); + T_ASSERT_EQ(*(char*)(ptr+PAGE_SIZE), 0, NULL); + T_ASSERT_EQ(dst[offset], 0, "null-term"); +} + +T_DECL(strlcat_overlap, "strlcat(3) with overlap: PR-20105548") +{ + char buffer[21]; + memset(buffer,'x',sizeof(buffer)); + buffer[0]='\0'; + buffer[20]='\0'; + + char *a = &buffer[0]; + char *b = &buffer[10]; + strlcat(a,b,10); + T_PASS("did not abort"); +}