]> git.saurik.com Git - apple/libc.git/blame - tests/strlcat.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / tests / strlcat.c
CommitLineData
5f125488
A
1#include <mach/mach_types.h>
2#include <sys/mman.h>
3#include <string.h>
4
974e3884
A
5#include <darwintest.h>
6
5f125488
A
7static const char* qbf = "The quick brown fox jumps over the lazy dog";
8static const char* lynx = "Lynx c.q. vos prikt bh: dag zwemjuf!";
9
974e3884 10T_DECL(strlcat, "strlcat(3)")
5f125488 11{
5f125488
A
12
13 void *ptr = mmap(NULL, PAGE_SIZE*2, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
974e3884 14 T_ASSERT_NE(ptr, MAP_FAILED, NULL);
5f125488 15
974e3884 16 T_ASSERT_POSIX_ZERO(mprotect(ptr+PAGE_SIZE, PAGE_SIZE, PROT_READ), NULL);
5f125488 17
974e3884 18 size_t offset = strlen(qbf)+strlen(lynx)+1;
5f125488
A
19 char *dst = (ptr+PAGE_SIZE)-offset;
20 strcpy(dst, qbf);
21
22 size_t res = strlcat(dst, lynx, offset);
974e3884
A
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");
5f125488
A
27
28 memset(ptr, '\0', PAGE_SIZE);
29
30 offset = strlen(qbf)+(strlen(lynx)/2)+1;
31 dst = (ptr+PAGE_SIZE)-offset;
32 strcpy(dst, qbf);
33
34 res = strlcat(dst, lynx, offset);
974e3884
A
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");
5f125488
A
40
41 memset(ptr, '\0', PAGE_SIZE);
42
43 offset = strlen(qbf)-4;
44 dst = (ptr+PAGE_SIZE)-offset;
45 strncpy(dst, qbf, offset);
46
47 res = strlcat(dst, lynx, offset);
974e3884
A
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");
52}
53
54T_DECL(strlcat_overlap, "strlcat(3) with overlap: PR-20105548")
55{
56 char buffer[21];
57 memset(buffer,'x',sizeof(buffer));
58 buffer[0]='\0';
59 buffer[20]='\0';
60
61 char *a = &buffer[0];
62 char *b = &buffer[10];
63 strlcat(a,b,10);
64 T_PASS("did not abort");
65}