-#include <bsdtests.h>
#include <mach/mach_types.h>
#include <sys/mman.h>
#include <string.h>
+#include <darwintest.h>
+
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);
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);
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");
+}