Skip to content

Commit b5e368f

Browse files
committed
strlim(): limit and parametrise message size
Truncate the messages silently in case of overflow.
1 parent 53ccb07 commit b5e368f

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/fread.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,16 @@ static inline int64_t clamp_i64t(int64_t x, int64_t lower, int64_t upper)
219219
/**
220220
* Helper for error and warning messages to extract an input line starting at
221221
* `*ch` and until an end of line, but no longer than `limit` characters.
222-
* This function returns the string copied into an internal static buffer. Cannot
223-
* be called more than twice per single printf() invocation.
224-
* Parameter `limit` cannot exceed 500.
222+
* This function returns the string copied into a caller-allocated buffer (typically on the stack).
223+
* Parameter `limit` should not exceed STRLIM_BUF_SIZE-1 (500).
225224
* The data might contain % characters. Therefore, careful to ensure that if the msg
226225
* is constructed manually (using say snprintf) that warning(), stop()
227226
* and Rprintf() are all called as warning(_("%s"), msg) and not warning(msg).
228227
*/
229-
static const char* strlim(const char *ch, char buf[static 501], size_t limit)
228+
#define STRLIM_BUF_SIZE 501
229+
static const char* strlim(const char *ch, char buf[static STRLIM_BUF_SIZE], size_t limit)
230230
{
231+
if (limit >= STRLIM_BUF_SIZE) limit = STRLIM_BUF_SIZE-1;
231232
char *ch2 = buf;
232233
for (size_t width = 0; (*ch > '\r' || (*ch != '\0' && *ch != '\r' && *ch != '\n')) && width < limit; width++) {
233234
*ch2++ = *ch++;
@@ -1776,7 +1777,7 @@ int freadMain(freadMainArgs _args)
17761777
if (ch >= eof) STOP(_("Input is either empty, fully whitespace, or skip has been set after the last non-whitespace."));
17771778
if (verbose) {
17781779
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[501]) {0}, 30));
1780+
DTPRINT(_(" Positioned on line %d starting: <<%s>>\n"), row1line, strlim(lineStart, (char[STRLIM_BUF_SIZE]) {0}, 30));
17801781
}
17811782
ch = pos = lineStart;
17821783
}
@@ -1967,7 +1968,7 @@ int freadMain(freadMainArgs _args)
19671968
if (!fill && tt != ncol) INTERNAL_STOP("first line has field count %d but expecting %d", tt, ncol); // # nocov
19681969
if (verbose) {
19691970
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[501]) {0}, 30));
1971+
tt, row1line, strlim(pos, (char[STRLIM_BUF_SIZE]) {0}, 30));
19711972
DTPRINT(_(" Quote rule picked = %d\n"), quoteRule);
19721973
DTPRINT(_(" fill=%s and the most number of columns found is %d\n"), fill ? "true" : "false", ncol);
19731974
}
@@ -2935,23 +2936,23 @@ int freadMain(freadMainArgs _args)
29352936
ch = skip_to_nextline(ch, eof);
29362937
while (ch < eof && isspace(*ch)) ch++;
29372938
if (ch == eof) {
2938-
DTWARN(_("Discarded single-line footer: <<%s>>"), strlim(skippedFooter, (char[501]) {0}, 500));
2939+
DTWARN(_("Discarded single-line footer: <<%s>>"), strlim(skippedFooter, (char[STRLIM_BUF_SIZE]) {0}, 500));
29392940
}
29402941
else {
29412942
ch = headPos;
29422943
int tt = countfields(&ch);
29432944
if (fill > 0) {
29442945
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[501]) {0}, 500));
2946+
DTi + row1line, ncol, tt, tt, strlim(skippedFooter, (char[STRLIM_BUF_SIZE]) {0}, 500));
29462947
} else {
29472948
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[501]) {0}, 500));
2949+
DTi + row1line, ncol, tt, strlim(skippedFooter, (char[STRLIM_BUF_SIZE]) {0}, 500));
29492950
}
29502951
}
29512952
}
29522953
}
29532954
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[501]) {0}, 500));
2955+
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[STRLIM_BUF_SIZE]) {0}, 500));
29552956
}
29562957

29572958
if (verbose) {

0 commit comments

Comments
 (0)