Skip to content

Commit 65efa7a

Browse files
committed
removed usage of static buffers
1 parent 6f05e4a commit 65efa7a

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

src/fread.c

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -205,27 +205,22 @@ static inline int64_t clamp_i64t(int64_t x, int64_t lower, int64_t upper) {
205205
* is constructed manually (using say snprintf) that warning(), stop()
206206
* and Rprintf() are all called as warning(_("%s"), msg) and not warning(msg).
207207
*/
208-
static const char* strlim(const char *ch, size_t limit) {
209-
static char buf[1002];
210-
static int flip = 0;
211-
char *ptr = buf + 501 * flip;
212-
flip = 1 - flip;
213-
char *ch2 = ptr;
208+
static const char* strlim(const char *ch, char buf[static 1002], size_t limit) {
209+
char *ch2 = buf;
214210
limit = imin(limit, 500);
215211
size_t width = 0;
216212
while ((*ch>'\r' || (*ch!='\0' && *ch!='\r' && *ch!='\n')) && width++<limit) {
217213
*ch2++ = *ch++;
218214
}
219215
*ch2 = '\0';
220-
return ptr;
216+
return buf;
221217
}
222218

223219
static const char *typeLetter = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
224220

225-
static char *typesAsString(int ncol) {
221+
static char *typesAsString(char str[static 101], int ncol) {
226222
int nLetters = strlen(typeLetter);
227223
if (NUMTYPE>nLetters) INTERNAL_STOP("NUMTYPE(%d) > nLetters(%d)", NUMTYPE, nLetters); // # nocov
228-
static char str[101];
229224
int i=0;
230225
if (ncol<=100) {
231226
for (; i<ncol; i++) str[i] = typeLetter[IGNORE_BUMP(type[i])];
@@ -413,10 +408,9 @@ double wallclock(void)
413408
* multiple threads at the same time, or hold on to the value returned for
414409
* extended periods of time.
415410
*/
416-
static const char* filesize_to_str(const size_t fsize)
411+
static const char* filesize_to_str(char output[static 100], const size_t fsize)
417412
{
418413
static const char suffixes[] = {'T', 'G', 'M', 'K'};
419-
static char output[100];
420414
for (int i = 0; i <= sizeof(suffixes); i++) {
421415
int shift = (sizeof(suffixes) - i) * 10;
422416
if ((fsize >> shift) == 0) continue;
@@ -426,18 +420,18 @@ static const char* filesize_to_str(const size_t fsize)
426420
}
427421
if (ndigits == 0 || (fsize == (fsize >> shift << shift))) {
428422
if (i < sizeof(suffixes)) {
429-
snprintf(output, sizeof(output), "%"PRIu64"%cB (%"PRIu64" bytes)", // # notranslate
423+
snprintf(output, 100, "%"PRIu64"%cB (%"PRIu64" bytes)", // # notranslate
430424
(fsize >> shift), suffixes[i], fsize);
431425
return output;
432426
}
433427
} else {
434-
snprintf(output, sizeof(output), "%.*f%cB (%"PRIu64" bytes)", // # notranslate
428+
snprintf(output, 100, "%.*f%cB (%"PRIu64" bytes)", // # notranslate
435429
ndigits, (double)fsize / (1LL << shift), suffixes[i], fsize);
436430
return output;
437431
}
438432
}
439433
if (fsize == 1) return "1 byte";
440-
snprintf(output, sizeof(output), "%"PRIu64" bytes", fsize); // # notranslate
434+
snprintf(output, 100, "%"PRIu64" bytes", fsize); // # notranslate
441435
return output;
442436
}
443437

@@ -1423,7 +1417,7 @@ int freadMain(freadMainArgs _args) {
14231417
}
14241418
fileSize = (size_t) stat_buf.st_size;
14251419
if (fileSize == 0) {close(fd); STOP(_("File is empty: %s"), fnam);}
1426-
if (verbose) DTPRINT(_(" File opened, size = %s.\n"), filesize_to_str(fileSize));
1420+
if (verbose) DTPRINT(_(" File opened, size = %s.\n"), filesize_to_str((char[100]) {}, fileSize));
14271421

14281422
// No MAP_POPULATE for faster nrows=10 and to make possible earlier progress bar in row count stage
14291423
// Mac doesn't appear to support MAP_POPULATE anyway (failed on CRAN when I tried).
@@ -1455,7 +1449,7 @@ int freadMain(freadMainArgs _args) {
14551449
if (GetFileSizeEx(hFile,&liFileSize)==0) { CloseHandle(hFile); STOP(_("GetFileSizeEx failed (returned 0) on file: %s"), fnam); }
14561450
fileSize = (size_t)liFileSize.QuadPart;
14571451
if (fileSize<=0) { CloseHandle(hFile); STOP(_("File is empty: %s"), fnam); }
1458-
if (verbose) DTPRINT(_(" File opened, size = %s.\n"), filesize_to_str(fileSize));
1452+
if (verbose) DTPRINT(_(" File opened, size = %s.\n"), filesize_to_str((char[100]) {}, fileSize));
14591453
HANDLE hMap=CreateFileMapping(hFile, NULL, PAGE_WRITECOPY, 0, 0, NULL);
14601454
if (hMap==NULL) { CloseHandle(hFile); STOP(_("This is Windows, CreateFileMapping returned error %lu for file %s"), GetLastError(), fnam); }
14611455
mmp = MapViewOfFile(hMap,FILE_MAP_COPY,0,0,fileSize); // fileSize must be <= hilo passed to CreateFileMapping above.
@@ -1464,7 +1458,7 @@ int freadMain(freadMainArgs _args) {
14641458
if (mmp == NULL) {
14651459
#endif
14661460
int nbit = 8*sizeof(char *); // #nocov
1467-
STOP(_("Opened %s file ok but could not memory map it. This is a %dbit process. %s."), filesize_to_str(fileSize), nbit, // # nocov
1461+
STOP(_("Opened %s file ok but could not memory map it. This is a %dbit process. %s."), filesize_to_str((char[100]) {}, fileSize), nbit, // # nocov
14681462
nbit<=32 ? _("Please upgrade to 64bit") : _("There is probably not enough contiguous virtual memory available")); // # nocov
14691463
}
14701464
sof = (const char*) mmp;
@@ -1561,7 +1555,7 @@ int freadMain(freadMainArgs _args) {
15611555
// # nocov start
15621556
if (!verbose)
15631557
DTPRINT(_("%s. Attempt to copy file in RAM failed."), msg);
1564-
STOP(_("Unable to allocate %s of contiguous virtual RAM."), filesize_to_str(fileSize));
1558+
STOP(_("Unable to allocate %s of contiguous virtual RAM."), filesize_to_str((char[100]) {}, fileSize));
15651559
// # nocov end
15661560
}
15671561
if (verbose)
@@ -1642,7 +1636,7 @@ int freadMain(freadMainArgs _args) {
16421636
if (ch>=eof) STOP(_("Input is either empty, fully whitespace, or skip has been set after the last non-whitespace."));
16431637
if (verbose) {
16441638
if (lineStart>ch) DTPRINT(_(" Moved forward to first non-blank line (%d)\n"), row1line);
1645-
DTPRINT(_(" Positioned on line %d starting: <<%s>>\n"), row1line, strlim(lineStart, 30));
1639+
DTPRINT(_(" Positioned on line %d starting: <<%s>>\n"), row1line, strlim(lineStart, (char[1002]) {}, 30));
16461640
}
16471641
ch = pos = lineStart;
16481642
}
@@ -1832,7 +1826,7 @@ int freadMain(freadMainArgs _args) {
18321826
if (!fill && tt!=ncol) INTERNAL_STOP("first line has field count %d but expecting %d", tt, ncol); // # nocov
18331827
if (verbose) {
18341828
DTPRINT(_(" Detected %d columns on line %d. This line is either column names or first data row. Line starts as: <<%s>>\n"),
1835-
tt, row1line, strlim(pos, 30));
1829+
tt, row1line, strlim(pos, (char[1002]) {}, 30));
18361830
DTPRINT(_(" Quote rule picked = %d\n"), quoteRule);
18371831
DTPRINT(_(" fill=%s and the most number of columns found is %d\n"), fill?"true":"false", ncol);
18381832
}
@@ -1849,7 +1843,7 @@ int freadMain(freadMainArgs _args) {
18491843
// # nocov start
18501844
if (!verbose)
18511845
DTPRINT(_("%s. Attempt to copy file in RAM failed."), msg);
1852-
STOP(_("Unable to allocate %s of contiguous virtual RAM."), filesize_to_str(fileSize));
1846+
STOP(_("Unable to allocate %s of contiguous virtual RAM."), filesize_to_str((char[100]) {}, fileSize));
18531847
// # nocov end
18541848
}
18551849
if (verbose)
@@ -1995,7 +1989,7 @@ int freadMain(freadMainArgs _args) {
19951989
memcpy(type, tmpType, ncol);
19961990
}
19971991
if (verbose && (bumped || jump==0 || jump==nJumps-1)) {
1998-
DTPRINT(_(" Type codes (jump %03d) : %s Quote rule %d\n"), jump, typesAsString(ncol), quoteRule);
1992+
DTPRINT(_(" Type codes (jump %03d) : %s Quote rule %d\n"), jump, typesAsString((char[101]) {}, ncol), quoteRule);
19991993
}
20001994
}
20011995

@@ -2090,7 +2084,7 @@ int freadMain(freadMainArgs _args) {
20902084
type[j] = tmpType[j];
20912085
}
20922086
}
2093-
if (verbose && bumped) DTPRINT(_(" Type codes (first row) : %s Quote rule %d\n"), typesAsString(ncol), quoteRule);
2087+
if (verbose && bumped) DTPRINT(_(" Type codes (first row) : %s Quote rule %d\n"), typesAsString((char[101]) {}, ncol), quoteRule);
20942088
}
20952089

20962090
estnrow=1;
@@ -2222,7 +2216,7 @@ int freadMain(freadMainArgs _args) {
22222216
rowSize8 += (size[j] & 8);
22232217
if (type[j] == CT_STRING) nStringCols++; else nNonStringCols++;
22242218
}
2225-
if (verbose) DTPRINT(_(" After %d type and %d drop user overrides : %s\n"), nUserBumped, ndrop, typesAsString(ncol));
2219+
if (verbose) DTPRINT(_(" After %d type and %d drop user overrides : %s\n"), nUserBumped, ndrop, typesAsString((char[101]) {}, ncol));
22262220
tColType = wallclock();
22272221
}
22282222

@@ -2689,7 +2683,7 @@ int freadMain(freadMainArgs _args) {
26892683
for (int i=0; i<ncol; i++) typeCounts[ IGNORE_BUMP(type[i]) ]++;
26902684

26912685
if (nTypeBump) {
2692-
if (verbose) DTPRINT(_(" %d out-of-sample type bumps: %s\n"), nTypeBump, typesAsString(ncol));
2686+
if (verbose) DTPRINT(_(" %d out-of-sample type bumps: %s\n"), nTypeBump, typesAsString((char[101]) {}, ncol));
26932687
rowSize1 = rowSize4 = rowSize8 = 0;
26942688
nStringCols = 0;
26952689
nNonStringCols = 0;
@@ -2725,7 +2719,7 @@ int freadMain(freadMainArgs _args) {
27252719

27262720
double tTot = tReread-t0; // tReread==tRead when there was no reread
27272721
if (verbose) DTPRINT(_("Read %"PRIu64" rows x %d columns from %s file in %02d:%06.3f wall clock time\n"),
2728-
(uint64_t)DTi, ncol-ndrop, filesize_to_str(fileSize), (int)tTot/60, fmod(tTot,60.0));
2722+
(uint64_t)DTi, ncol-ndrop, filesize_to_str((char[100]) {}, fileSize), (int)tTot/60, fmod(tTot,60.0));
27292723

27302724
//*********************************************************************************************
27312725
// [12] Finalize the datatable
@@ -2750,23 +2744,23 @@ int freadMain(freadMainArgs _args) {
27502744
while (ch<eof && *ch!='\n' && *ch!='\r') ch++;
27512745
while (ch<eof && isspace(*ch)) ch++;
27522746
if (ch==eof) {
2753-
DTWARN(_("Discarded single-line footer: <<%s>>"), strlim(skippedFooter,500));
2747+
DTWARN(_("Discarded single-line footer: <<%s>>"), strlim(skippedFooter, (char[1002]) {}, 500));
27542748
}
27552749
else {
27562750
ch = headPos;
27572751
int tt = countfields(&ch);
27582752
if (fill>0) {
27592753
DTWARN(_("Stopped early on line %"PRIu64". 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>>"),
2760-
(uint64_t)DTi+row1line, ncol, tt, tt, strlim(skippedFooter,500));
2754+
(uint64_t)DTi+row1line, ncol, tt, tt, strlim(skippedFooter, (char[1002]) {}, 500));
27612755
} else {
27622756
DTWARN(_("Stopped early on line %"PRIu64". Expected %d fields but found %d. Consider fill=TRUE. First discarded non-empty line: <<%s>>"),
2763-
(uint64_t)DTi+row1line, ncol, tt, strlim(skippedFooter,500));
2757+
(uint64_t)DTi+row1line, ncol, tt, strlim(skippedFooter, (char[1002]) {}, 500));
27642758
}
27652759
}
27662760
}
27672761
}
27682762
if (quoteRuleBumpedCh!=NULL && quoteRuleBumpedCh<headPos) {
2769-
DTWARN(_("Found and resolved improper quoting out-of-sample. First healed line %"PRIu64": <<%s>>. If the fields are not quoted (e.g. field separator does not appear within any field), try quote=\"\" to avoid this warning."), (uint64_t)quoteRuleBumpedLine, strlim(quoteRuleBumpedCh, 500));
2763+
DTWARN(_("Found and resolved improper quoting out-of-sample. First healed line %"PRIu64": <<%s>>. If the fields are not quoted (e.g. field separator does not appear within any field), try quote=\"\" to avoid this warning."), (uint64_t)quoteRuleBumpedLine, strlim(quoteRuleBumpedCh, (char[1002]) {}, 500));
27702764
}
27712765

27722766
if (verbose) {

0 commit comments

Comments
 (0)