/src/r-oss-fuzz/harnesses/sprintf.c
Line | Count | Source |
1 | | /* Exercise R's C-style format-string parser and argument dispatch. */ |
2 | | |
3 | | #include <stdint.h> |
4 | | #include <string.h> |
5 | | |
6 | | #include "common.h" |
7 | | |
8 | 4.39k | #define FUZZ_MAX_INPUT (1024 * 16) |
9 | 2.18k | #define N_CALLS 4 |
10 | | |
11 | | static SEXP format; |
12 | | static SEXP calls[N_CALLS]; |
13 | | |
14 | | int fuzz_target_init(int *argc, char ***argv) |
15 | 2 | { |
16 | 2 | (void)argc; |
17 | 2 | (void)argv; |
18 | 2 | fuzz_init_r(); |
19 | | |
20 | 2 | SEXP string, integer, real; |
21 | 2 | Rf_protect(format = Rf_allocVector(STRSXP, 1)); |
22 | 2 | Rf_protect(string = Rf_mkString("fuzz")); |
23 | 2 | Rf_protect(integer = Rf_ScalarInteger(-12345)); |
24 | 2 | Rf_protect(real = Rf_ScalarReal(3.141592653589793)); |
25 | | |
26 | 2 | SEXP sprintf = Rf_install("sprintf"); |
27 | 2 | Rf_protect(calls[0] = Rf_lang3(sprintf, format, string)); |
28 | 2 | Rf_protect(calls[1] = Rf_lang3(sprintf, format, integer)); |
29 | 2 | Rf_protect(calls[2] = Rf_lang3(sprintf, format, real)); |
30 | 2 | Rf_protect(calls[3] = Rf_lang5(sprintf, format, integer, real, string)); |
31 | 2 | fuzz_label_calls(calls, N_CALLS); |
32 | 2 | SET_STRING_ELT(format, 0, Rf_mkChar("%s")); |
33 | 2 | return 0; |
34 | 2 | } |
35 | | |
36 | | int fuzz_target_run(const uint8_t *data, size_t size) |
37 | 2.20k | { |
38 | 2.20k | if (size < 2 || size > FUZZ_MAX_INPUT || memchr(data + 1, '\0', size - 1)) |
39 | 24 | return 0; |
40 | | |
41 | 2.17k | char buffer[FUZZ_MAX_INPUT]; |
42 | 2.17k | memcpy(buffer, data + 1, size - 1); |
43 | 2.17k | buffer[size - 1] = '\0'; |
44 | 2.17k | if (!fuzz_set_string(format, buffer)) |
45 | 0 | return 0; |
46 | | |
47 | 2.17k | fuzz_eval_silent(calls[data[0] % N_CALLS], R_GlobalEnv); |
48 | 2.17k | return 0; |
49 | 2.17k | } |