Skip to content

Commit 037bc89

Browse files
removed usage of static buffers (#7005)
* copied commit from local branch * restore uint64_t * new signature in two new call sites * restore diff lost to sloppy merge * corrected error from conflict resolution * scrapped and redone --------- Co-authored-by: Michael Chirico <[email protected]> Co-authored-by: Michael Chirico <[email protected]>
1 parent ab7c931 commit 037bc89

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

src/fread.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,14 @@ static inline int64_t clamp_i64t(int64_t x, int64_t lower, int64_t upper) {
210210
* is constructed manually (using say snprintf) that warning(), stop()
211211
* and Rprintf() are all called as warning(_("%s"), msg) and not warning(msg).
212212
*/
213-
static const char* strlim(const char *ch, size_t limit) {
214-
static char buf[1002];
215-
static int flip = 0;
216-
char *ptr = buf + 501 * flip;
217-
flip = 1 - flip;
218-
char *ch2 = ptr;
219-
limit = imin(limit, 500);
213+
static const char* strlim(const char *ch, char buf[static 500], size_t limit) {
214+
char *ch2 = buf;
220215
size_t width = 0;
221216
while ((*ch > '\r' || (*ch != '\0' && *ch != '\r' && *ch != '\n')) && width++ < limit) {
222217
*ch2++ = *ch++;
223218
}
224219
*ch2 = '\0';
225-
return ptr;
220+
return buf;
226221
}
227222

228223
static const char *typeLetter = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
@@ -1638,7 +1633,7 @@ int freadMain(freadMainArgs _args) {
16381633
if (ch >= eof) STOP(_("Input is either empty, fully whitespace, or skip has been set after the last non-whitespace."));
16391634
if (verbose) {
16401635
if (lineStart > ch) DTPRINT(_(" Moved forward to first non-blank line (%d)\n"), row1line);
1641-
DTPRINT(_(" Positioned on line %d starting: <<%s>>\n"), row1line, strlim(lineStart, 30));
1636+
DTPRINT(_(" Positioned on line %d starting: <<%s>>\n"), row1line, strlim(lineStart, (char[500]) {}, 30));
16421637
}
16431638
ch = pos = lineStart;
16441639
}
@@ -1829,7 +1824,7 @@ int freadMain(freadMainArgs _args) {
18291824
if (!fill && tt != ncol) INTERNAL_STOP("first line has field count %d but expecting %d", tt, ncol); // # nocov
18301825
if (verbose) {
18311826
DTPRINT(_(" Detected %d columns on line %d. This line is either column names or first data row. Line starts as: <<%s>>\n"),
1832-
tt, row1line, strlim(pos, 30));
1827+
tt, row1line, strlim(pos, (char[500]) {}, 30));
18331828
DTPRINT(_(" Quote rule picked = %d\n"), quoteRule);
18341829
DTPRINT(_(" fill=%s and the most number of columns found is %d\n"), fill ? "true" : "false", ncol);
18351830
}
@@ -2759,23 +2754,23 @@ int freadMain(freadMainArgs _args) {
27592754
while (ch < eof && *ch != '\n' && *ch != '\r') ch++;
27602755
while (ch < eof && isspace(*ch)) ch++;
27612756
if (ch == eof) {
2762-
DTWARN(_("Discarded single-line footer: <<%s>>"), strlim(skippedFooter, 500));
2757+
DTWARN(_("Discarded single-line footer: <<%s>>"), strlim(skippedFooter, (char[500]) {}, 500));
27632758
}
27642759
else {
27652760
ch = headPos;
27662761
int tt = countfields(&ch);
27672762
if (fill > 0) {
27682763
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>>"),
2769-
DTi+row1line, ncol, tt, tt, strlim(skippedFooter, 500));
2764+
DTi+row1line, ncol, tt, tt, strlim(skippedFooter, (char[500]) {}, 500));
27702765
} else {
27712766
DTWARN(_("Stopped early on line %"PRId64". Expected %d fields but found %d. Consider fill=TRUE. First discarded non-empty line: <<%s>>"),
2772-
DTi+row1line, ncol, tt, strlim(skippedFooter, 500));
2767+
DTi+row1line, ncol, tt, strlim(skippedFooter, (char[500]) {}, 500));
27732768
}
27742769
}
27752770
}
27762771
}
27772772
if (quoteRuleBumpedCh != NULL && quoteRuleBumpedCh<headPos) {
2778-
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, 500));
2773+
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]) {}, 500));
27792774
}
27802775

27812776
if (verbose) {

0 commit comments

Comments
 (0)