Skip to content

Commit 53ccb07

Browse files
committed
strlim: avoid stack overflow
Since the limit is up to 500 characters, make sure to allocate 501 bytes, including the terminator at the end, avoiding the stack overflow: data.table::fread(text = paste0( strrep("mary had a little lamb\n", 100), strrep("a", 500), "\n", "a") ) ==5358==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffd0f20c7e4 at pc 0x7f06826bb311 bp 0x7ffd0f20ad90 sp 0x7ffd0f20ad88 WRITE of size 1 at 0x7ffd0f20c7e4 thread T0 #0 0x7f06826bb310 in strlim /tmp/RtmpvbbbcM/R.INSTALLd607e4c6707/data.table/src/fread.c:235 #1 0x7f06826bb310 in freadMain /tmp/RtmpvbbbcM/R.INSTALLd607e4c6707/data.table/src/fread.c:2947 #2 0x7f06826c2d62 in freadR /tmp/RtmpvbbbcM/R.INSTALLd607e4c6707/data.table/src/freadR.c:229
1 parent 1685a3b commit 53ccb07

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/fread.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ static inline int64_t clamp_i64t(int64_t x, int64_t lower, int64_t upper)
226226
* is constructed manually (using say snprintf) that warning(), stop()
227227
* and Rprintf() are all called as warning(_("%s"), msg) and not warning(msg).
228228
*/
229-
static const char* strlim(const char *ch, char buf[static 500], size_t limit)
229+
static const char* strlim(const char *ch, char buf[static 501], size_t limit)
230230
{
231231
char *ch2 = buf;
232232
for (size_t width = 0; (*ch > '\r' || (*ch != '\0' && *ch != '\r' && *ch != '\n')) && width < limit; width++) {
@@ -1776,7 +1776,7 @@ int freadMain(freadMainArgs _args)
17761776
if (ch >= eof) STOP(_("Input is either empty, fully whitespace, or skip has been set after the last non-whitespace."));
17771777
if (verbose) {
17781778
if (lineStart > ch) DTPRINT(_(" Moved forward to first non-blank line (%d)\n"), row1line);
1779-
DTPRINT(_(" Positioned on line %d starting: <<%s>>\n"), row1line, strlim(lineStart, (char[500]) {0}, 30));
1779+
DTPRINT(_(" Positioned on line %d starting: <<%s>>\n"), row1line, strlim(lineStart, (char[501]) {0}, 30));
17801780
}
17811781
ch = pos = lineStart;
17821782
}
@@ -1967,7 +1967,7 @@ int freadMain(freadMainArgs _args)
19671967
if (!fill && tt != ncol) INTERNAL_STOP("first line has field count %d but expecting %d", tt, ncol); // # nocov
19681968
if (verbose) {
19691969
DTPRINT(_(" Detected %d columns on line %d. This line is either column names or first data row. Line starts as: <<%s>>\n"),
1970-
tt, row1line, strlim(pos, (char[500]) {0}, 30));
1970+
tt, row1line, strlim(pos, (char[501]) {0}, 30));
19711971
DTPRINT(_(" Quote rule picked = %d\n"), quoteRule);
19721972
DTPRINT(_(" fill=%s and the most number of columns found is %d\n"), fill ? "true" : "false", ncol);
19731973
}
@@ -2935,23 +2935,23 @@ int freadMain(freadMainArgs _args)
29352935
ch = skip_to_nextline(ch, eof);
29362936
while (ch < eof && isspace(*ch)) ch++;
29372937
if (ch == eof) {
2938-
DTWARN(_("Discarded single-line footer: <<%s>>"), strlim(skippedFooter, (char[500]) {0}, 500));
2938+
DTWARN(_("Discarded single-line footer: <<%s>>"), strlim(skippedFooter, (char[501]) {0}, 500));
29392939
}
29402940
else {
29412941
ch = headPos;
29422942
int tt = countfields(&ch);
29432943
if (fill > 0) {
29442944
DTWARN(_("Stopped early on line %"PRId64". Expected %d fields but found %d. Consider fill=%d or even more based on your knowledge of the input file. Use fill=Inf for reading the whole file for detecting the number of fields. First discarded non-empty line: <<%s>>"),
2945-
DTi + row1line, ncol, tt, tt, strlim(skippedFooter, (char[500]) {0}, 500));
2945+
DTi + row1line, ncol, tt, tt, strlim(skippedFooter, (char[501]) {0}, 500));
29462946
} else {
29472947
DTWARN(_("Stopped early on line %"PRId64". Expected %d fields but found %d. Consider fill=TRUE. First discarded non-empty line: <<%s>>"),
2948-
DTi + row1line, ncol, tt, strlim(skippedFooter, (char[500]) {0}, 500));
2948+
DTi + row1line, ncol, tt, strlim(skippedFooter, (char[501]) {0}, 500));
29492949
}
29502950
}
29512951
}
29522952
}
29532953
if (quoteRuleBumpedCh != NULL && quoteRuleBumpedCh < headPos) {
2954-
DTWARN(_("Found and resolved improper quoting out-of-sample. First healed line %"PRId64": <<%s>>. If the fields are not quoted (e.g. field separator does not appear within any field), try quote=\"\" to avoid this warning."), quoteRuleBumpedLine, strlim(quoteRuleBumpedCh, (char[500]) {0}, 500));
2954+
DTWARN(_("Found and resolved improper quoting out-of-sample. First healed line %"PRId64": <<%s>>. If the fields are not quoted (e.g. field separator does not appear within any field), try quote=\"\" to avoid this warning."), quoteRuleBumpedLine, strlim(quoteRuleBumpedCh, (char[501]) {0}, 500));
29552955
}
29562956

29572957
if (verbose) {

0 commit comments

Comments
 (0)