]>
git.saurik.com Git - apple/libc.git/blob - tests/strlcat.c
1 #include <mach/mach_types.h>
5 #include <darwintest.h>
7 static const char* qbf
= "The quick brown fox jumps over the lazy dog";
8 static const char* lynx
= "Lynx c.q. vos prikt bh: dag zwemjuf!";
10 T_DECL(strlcat
, "strlcat(3)")
13 void *ptr
= mmap(NULL
, PAGE_SIZE
*2, PROT_READ
|PROT_WRITE
, MAP_ANON
|MAP_PRIVATE
, -1, 0);
14 T_ASSERT_NE(ptr
, MAP_FAILED
, NULL
);
16 T_ASSERT_POSIX_ZERO(mprotect(ptr
+PAGE_SIZE
, PAGE_SIZE
, PROT_READ
), NULL
);
18 size_t offset
= strlen(qbf
)+strlen(lynx
)+1;
19 char *dst
= (ptr
+PAGE_SIZE
)-offset
;
22 size_t res
= strlcat(dst
, lynx
, offset
);
23 T_ASSERT_EQ(res
, offset
-1, "strlcat");
24 T_ASSERT_EQ(memcmp(dst
, qbf
, strlen(qbf
)), 0, NULL
);
25 T_ASSERT_EQ(memcmp(dst
+strlen(qbf
), lynx
, strlen(lynx
)), 0, NULL
);
26 T_ASSERT_EQ(dst
[offset
], 0, "null-term");
28 memset(ptr
, '\0', PAGE_SIZE
);
30 offset
= strlen(qbf
)+(strlen(lynx
)/2)+1;
31 dst
= (ptr
+PAGE_SIZE
)-offset
;
34 res
= strlcat(dst
, lynx
, offset
);
35 T_ASSERT_EQ(res
, strlen(qbf
)+strlen(lynx
), "strlcat");
36 T_ASSERT_EQ(memcmp(dst
, qbf
, strlen(qbf
)), 0, NULL
);
37 T_ASSERT_EQ(memcmp(dst
+strlen(qbf
), lynx
, offset
-strlen(qbf
)-1), 0, NULL
);
38 T_ASSERT_EQ(*(char*)(ptr
+PAGE_SIZE
), 0, NULL
);
39 T_ASSERT_EQ(dst
[offset
], 0, "null-term");
41 memset(ptr
, '\0', PAGE_SIZE
);
43 offset
= strlen(qbf
)-4;
44 dst
= (ptr
+PAGE_SIZE
)-offset
;
45 strncpy(dst
, qbf
, offset
);
47 res
= strlcat(dst
, lynx
, offset
);
48 T_ASSERT_EQ(res
, offset
+strlen(lynx
), "strlcat");
49 T_ASSERT_EQ(memcmp(dst
, qbf
, offset
), 0, NULL
);
50 T_ASSERT_EQ(*(char*)(ptr
+PAGE_SIZE
), 0, NULL
);
51 T_ASSERT_EQ(dst
[offset
], 0, "null-term");
54 T_DECL(strlcat_overlap
, "strlcat(3) with overlap: PR-20105548")
57 memset(buffer
,'x',sizeof(buffer
));
62 char *b
= &buffer
[10];
64 T_PASS("did not abort");