]>
Commit | Line | Data |
---|---|---|
b061a43b A |
1 | #include <sys/types.h> |
2 | #include <sys/resource.h> | |
3 | #include <math.h> | |
4 | #include <stdio.h> | |
5 | #include <stdint.h> | |
6 | #include <string.h> | |
7 | #include <time.h> | |
8 | #include <stdlib.h> | |
9 | #include <os/assumes.h> | |
10 | ||
11 | #include "darwintest.h" | |
12 | #include "darwintest_utils.h" | |
13 | ||
14 | static void crash_callback(const char *str) { | |
15 | T_PASS("Crashed with \"%s\"", str); | |
16 | T_END; | |
17 | } | |
18 | ||
19 | T_DECL(sprintf_percent_n, "Test of %n") | |
20 | { | |
21 | char str[1024]; | |
22 | int len, ret; | |
23 | ||
24 | char *fmt = "%010d%n"; | |
25 | ||
26 | T_EXPECT_POSIX_SUCCESS((ret = snprintf(str, sizeof(str), fmt, 0, &len)), NULL); | |
27 | T_EXPECT_EQ(len, ret, NULL); | |
28 | ||
29 | char fmt_buf[32]; | |
30 | strlcpy(fmt_buf, fmt, sizeof(fmt_buf)); | |
31 | ||
32 | os_set_crash_callback(crash_callback); | |
33 | snprintf(str, sizeof(str), fmt_buf, 0, &len); | |
34 | T_ASSERT_FAIL("Should have crashed on dynamic %%n"); | |
35 | } | |
36 | ||
37 | #if !TARGET_OS_IPHONE | |
38 | #define STRSIZE (1024 * 1024 * 256) | |
39 | ||
40 | T_DECL(printf_PR_30663523, "Test for PR-30663523", | |
41 | T_META_CHECK_LEAKS(NO)) | |
42 | { | |
43 | char *temp_path; | |
44 | asprintf(&temp_path, "%s/%s", dt_tmpdir(), "big_file"); | |
45 | ||
46 | { | |
47 | char *x = calloc(1, 0x80000001); | |
48 | memset(x, 0x41, 0x80000001); | |
49 | ||
50 | FILE *f = fopen(temp_path, "w"); | |
51 | int len = fprintf(f, "%s", x); | |
52 | T_EXPECT_EQ(len, EOF, "fprintf should return EOF when string is longer than INT_MAX"); | |
53 | fclose(f); | |
54 | } | |
55 | ||
56 | { | |
57 | char *x = calloc(1, STRSIZE); | |
58 | memset(x, 0x41, STRSIZE - 1); | |
59 | ||
60 | FILE *f = fopen(temp_path, "w"); | |
61 | int len = fprintf(f, "%s%s%s%s%s%s%s%s%s%s", x,x,x,x,x,x,x,x,x,x); | |
62 | T_EXPECT_EQ(len, EOF, "fprintf should return EOF when output string is longer than INT_MAX"); | |
63 | fclose(f); | |
64 | } | |
65 | } | |
66 | #endif // !TARGET_OS_IPHONE |