Coverage Report

Created: 2026-11-30 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/r-oss-fuzz/harnesses/readtable.c
Line
Count
Source
1
/*
2
 * libFuzzer harness for read.table() and friends -- the tabular readers
3
 * most untrusted CSV enters R through.
4
 *
5
 * The scan target covers scan.c, but read.table() wraps it in C code of
6
 * its own in src/library/utils/src/io.c that scan never reaches:
7
 * readtablehead (the header/line pre-reader that tracks quotes and
8
 * comments across physical lines), countfields (count.fields), and
9
 * writetable (the quoting and encoding of write.table()).  Each input is
10
 * handed to one of those paths as the text of a file, under a
11
 * configuration chosen by the first byte; the readers that succeed hand
12
 * their data frame to write.table() so the writer sees real, fuzz-shaped
13
 * columns rather than only hand-built ones.
14
 *
15
 * Writers go to nullfile(): the C code runs in full, nothing hits the
16
 * fuzzer's stdout.
17
 */
18
19
#include <stdint.h>
20
#include <string.h>
21
22
#include "common.h"
23
24
10.1k
#define FUZZ_MAX_INPUT (1024 * 64)
25
5.44k
#define N_CALLS 20
26
27
/* Most cells a padding slot may be handed (see cell_bound).  The cost of
28
 * a cell grows with the number of columns: under ASAN, 1M cells took 2 s
29
 * as 250 columns and 12 s as 30000, the widest a 64 KiB input allows,
30
 * and 4M took 18 s as 30000 columns. */
31
3.69k
#define FUZZ_MAX_CELLS (1024 * 1024)
32
33
/* Slot layout:
34
 *   [0,12)   read.table() and its csv/csv2/delim presets under assorted
35
 *            separators, quoting, comments, NA strings, fill/flush,
36
 *            escapes, encodings, colClasses and row.names handling
37
 *   [12,15)  count.fields() on a textConnection of the input
38
 *   [15,20)  readers chained into write.table()/write.csv(): quoting
39
 *            methods, factor levels, NA and decimal marks, line endings,
40
 *            and a re-encoding fileEncoding
41
 * Every connection a slot opens is closed before it returns: R caps the
42
 * connection table at 128 slots. */
43
static const char *const sources[N_CALLS] = {
44
    "function(x) read.table(text = x)",
45
    "function(x) read.csv(text = x)",
46
    "function(x) read.csv2(text = x)",
47
    "function(x) read.delim(text = x)",
48
    "function(x) read.table(text = x, header = TRUE, sep = \",\", "
49
    "quote = \"\\\"'\", comment.char = \"#\", fill = TRUE)",
50
    "function(x) read.table(text = x, sep = \",\", allowEscapes = TRUE, "
51
    "strip.white = TRUE, blank.lines.skip = FALSE)",
52
    "function(x) read.table(text = x, header = TRUE, row.names = 1, "
53
    "check.names = TRUE)",
54
    "function(x) read.table(text = x, colClasses = \"character\", "
55
    "nrows = 100, skip = 1, fill = TRUE)",
56
    "function(x) read.table(text = x, header = TRUE, "
57
    "na.strings = c(\"NA\", \"\", \"-\", \"?\"), stringsAsFactors = TRUE)",
58
    "function(x) read.table(text = x, flush = TRUE, numerals = \"no.loss\", "
59
    "dec = \",\")",
60
    "function(x) read.csv(text = x, encoding = \"UTF-8\", skipNul = TRUE)",
61
    "function(x) read.csv(text = x, encoding = \"latin1\", header = FALSE, "
62
    "col.names = paste0(\"V\", 1:3), fill = TRUE)",
63
    "function(x) { con <- textConnection(x); on.exit(close(con)); "
64
    "count.fields(con, sep = \",\", quote = \"\\\"\", comment.char = \"#\") }",
65
    "function(x) { con <- textConnection(x); on.exit(close(con)); "
66
    "count.fields(con, sep = \"\", blank.lines.skip = FALSE, skip = 1) }",
67
    "function(x) { con <- textConnection(x); on.exit(close(con)); "
68
    "count.fields(con, sep = \";\", quote = \"'\", comment.char = \"\") }",
69
    "function(x) { d <- read.csv(text = x, stringsAsFactors = TRUE); "
70
    "write.table(d, nullfile(), sep = \",\", qmethod = \"double\") }",
71
    "function(x) { d <- read.table(text = x, fill = TRUE); "
72
    "write.csv(d, nullfile(), row.names = FALSE, na = \"\") }",
73
    "function(x) { d <- read.csv2(text = x); "
74
    "write.table(d, nullfile(), quote = FALSE, dec = \",\", eol = \"\\r\\n\", "
75
    "col.names = NA) }",
76
    "function(x) { d <- read.delim(text = x); "
77
    "write.table(d, nullfile(), qmethod = \"escape\", quote = seq_along(d)) }",
78
    "function(x) { d <- read.csv(text = x, encoding = \"UTF-8\"); "
79
    "write.csv(d, nullfile(), fileEncoding = \"UTF-16LE\") }",
80
};
81
82
/* The slots whose reader pads short rows: fill = TRUE, given or by
83
 * default (read.csv, read.csv2, read.delim, and read.table() with
84
 * blank.lines.skip = FALSE).  Keep in step with sources[]. */
85
static const int pads[N_CALLS] = {
86
    0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1,     /* [0,12)  readers */
87
    0, 0, 0,                                /* [12,15) count.fields() */
88
    1, 1, 1, 1, 1,                          /* [15,20) readers into writers */
89
};
90
91
/*
92
 * Upper bound on the cells a padding slot makes of text.  With fill,
93
 * read.table() pads every row to the most fields in its first five
94
 * lines, so one wide line over many short ones is quadratic in the
95
 * input: 43 KB of tabs and carriage returns read as 10574 rows of 4458
96
 * columns, 47M cells (oss-fuzz testcase 6363833382535168).
97
 *
98
 * There are at most as many rows as physical lines, which \n and \r
99
 * each end.  A row has at most one field more than the bytes on its
100
 * lines that can end a field in some slot: the separators (sep = ""
101
 * splits at blanks, and at NBSP in a Latin-1 locale) and the quote
102
 * characters, as sep = "" reads "a"b as two fields.  A row spans lines
103
 * only inside a quote, and a line without a quote character is then
104
 * quoted from end to end, so nothing on it ends a field: the counts on
105
 * the lines that hold a quote character bound every row that spans
106
 * lines, whatever each slot takes as its quotes.  Slot 11's three
107
 * col.names are the one width that is not counted.
108
 */
109
static size_t cell_bound(const char *text)
110
3.69k
{
111
3.69k
    size_t lines = 1, widest = 0, quoted = 0, ends = 0;
112
3.69k
    int has_quote = 0;
113
114
5.88M
    for (const unsigned char *p = (const unsigned char *)text; ; p++) {
115
5.88M
        if (*p == '\0' || *p == '\n' || *p == '\r') {
116
349k
            if (ends > widest)
117
3.46k
                widest = ends;
118
349k
            if (has_quote)
119
6.43k
                quoted += ends;
120
349k
            if (*p == '\0')
121
3.69k
                break;
122
345k
            lines++;
123
345k
            ends = 0;
124
345k
            has_quote = 0;
125
5.53M
        } else if (*p == ' ' || *p == '\t' || *p == ',' || *p == ';'
126
3.79M
                   || *p == 0xa0) {
127
1.75M
            ends++;
128
3.78M
        } else if (*p == '"' || *p == '\'') {
129
34.5k
            ends++;
130
34.5k
            has_quote = 1;
131
34.5k
        }
132
5.88M
    }
133
134
3.69k
    return lines * (1 + (quoted > widest ? quoted : widest));
135
3.69k
}
136
137
static SEXP x_text;
138
static SEXP calls[N_CALLS];
139
140
int fuzz_target_init(int *argc, char ***argv)
141
12
{
142
12
    (void)argc;
143
12
    (void)argv;
144
12
    fuzz_init_r();
145
146
    /* Reusable text container -- each iteration swaps its CHARSXP. */
147
12
    Rf_protect(x_text = Rf_allocVector(STRSXP, 1));
148
149
168
    for (int i = 0; i < N_CALLS; i++) {
150
156
        SEXP wrapper;
151
156
        Rf_protect(wrapper = fuzz_make_wrapper(sources[i]));
152
156
        Rf_protect(calls[i] = Rf_lang2(wrapper, x_text));
153
156
    }
154
155
12
    fuzz_label_calls(calls, N_CALLS);
156
157
    /* Warmup: load utils and prime read.table's method dispatch. */
158
12
    SET_STRING_ELT(x_text, 0, Rf_mkChar("a,b,c\n1,2,x\n4,NA,\"y\"\n"));
159
168
    for (int i = 0; i < N_CALLS; i++)
160
156
        fuzz_eval_silent(calls[i], R_GlobalEnv);
161
162
12
    return 0;
163
12
}
164
165
int fuzz_target_run(const uint8_t *data, size_t size)
166
5.09k
{
167
5.09k
    if (size < 2 || size > FUZZ_MAX_INPUT || memchr(data + 1, '\0', size - 1))
168
6
        return 0;
169
170
5.09k
    char buffer[FUZZ_MAX_INPUT];
171
5.09k
    memcpy(buffer, data + 1, size - 1);
172
5.09k
    buffer[size - 1] = '\0';
173
5.09k
    int slot = data[0] % N_CALLS;
174
5.09k
    if (pads[slot] && cell_bound(buffer) > FUZZ_MAX_CELLS)
175
1
        return 0;
176
5.09k
    if (!fuzz_set_string(x_text, buffer))
177
0
        return 0;
178
179
5.09k
    fuzz_eval_silent(calls[slot], R_GlobalEnv);
180
5.09k
    return 0;
181
5.09k
}