Skip to content

Commit 6f05e4a

Browse files
authored
size_t cast cleanup (#6982)
* `size_t` usage and casting cleanup * fixed overzealous cast removal on double/pointer arithmetic * implemented suggested fixes * renamed function `clamp_szt`
1 parent 2a4ed52 commit 6f05e4a

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/fread.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ static inline uint64_t umin(uint64_t a, uint64_t b) { return a < b ? a : b; }
190190
static inline int64_t imin( int64_t a, int64_t b) { return a < b ? a : b; }
191191

192192
/** Return value of `x` clamped to the range [upper, lower] */
193-
static inline int64_t clamp_szt(int64_t x, int64_t lower, int64_t upper) {
193+
static inline int64_t clamp_i64t(int64_t x, int64_t lower, int64_t upper) {
194194
return x < lower ? lower : x > upper? upper : x;
195195
}
196196

@@ -211,7 +211,7 @@ static const char* strlim(const char *ch, size_t limit) {
211211
char *ptr = buf + 501 * flip;
212212
flip = 1 - flip;
213213
char *ch2 = ptr;
214-
if (limit>500) limit=500;
214+
limit = imin(limit, 500);
215215
size_t width = 0;
216216
while ((*ch>'\r' || (*ch!='\0' && *ch!='\r' && *ch!='\n')) && width++<limit) {
217217
*ch2++ = *ch++;
@@ -923,7 +923,7 @@ static void parse_double_hexadecimal(FieldParseContext *ctx)
923923
acc = (acc << 4) + digit;
924924
ch++;
925925
}
926-
size_t ndigits = (uint_fast8_t)(ch - ch0);
926+
ptrdiff_t ndigits = ch - ch0;
927927
if (ndigits > 13 || !(*ch=='p' || *ch=='P')) return;
928928
acc <<= (13 - ndigits) * 4;
929929
ch += 1 + (Eneg = ch[1]=='-') + (ch[1]=='+');
@@ -1876,7 +1876,7 @@ int freadMain(freadMainArgs _args) {
18761876
int64_t estnrow=1;
18771877
int64_t allocnrow=0; // Number of rows in the allocated DataTable
18781878
double meanLineLen=0.0; // Average length (in bytes) of a single line in the input file
1879-
size_t bytesRead=0; // Bytes in the data section (i.e. excluding column names, header and footer, if any)
1879+
ptrdiff_t bytesRead=0; // Bytes in the data section (i.e. excluding column names, header and footer, if any)
18801880
{
18811881
if (verbose) DTPRINT(_("[07] Detect column types, dec, good nrow estimate and whether first row is column names\n"));
18821882
if (verbose && args.header!=NA_BOOL8) DTPRINT(_(" 'header' changed by user from 'auto' to %s\n"), args.header?"true":"false");
@@ -1940,8 +1940,8 @@ int freadMain(freadMainArgs _args) {
19401940
}
19411941
firstRowStart = ch;
19421942
} else {
1943-
ch = (jump == nJumps-1) ? eof - (size_t)(0.5*jump0size) : // to almost-surely sample the last line
1944-
pos + (size_t)jump*((size_t)(eof-pos)/(size_t)(nJumps-1));
1943+
ch = (jump == nJumps-1) ? eof - (ptrdiff_t)(0.5*jump0size) : // to almost-surely sample the last line
1944+
pos + jump*((eof-pos)/(nJumps-1));
19451945
ch = nextGoodLine(ch, ncol);
19461946
}
19471947
if (ch<lastRowEnd) ch=lastRowEnd; // Overlap when apx 1,200 lines (just over 11*100) with short lines at the beginning and longer lines near the end, #2157
@@ -1978,7 +1978,7 @@ int freadMain(freadMainArgs _args) {
19781978
if (jump==0 && bumped) {
19791979
// apply bumps after each line in the first jump from the start in case invalid line stopped early on is in the first 100 lines.
19801980
// otherwise later jumps must complete fully before their bumps are applied. Invalid lines in those are more likely to be due to bad jump start.
1981-
memcpy(type, tmpType, (size_t)ncol);
1981+
memcpy(type, tmpType, ncol);
19821982
bumped = false; // detect_types() only updates &bumped when it's true. So reset to false here.
19831983
}
19841984
}
@@ -1992,7 +1992,7 @@ int freadMain(freadMainArgs _args) {
19921992
if (bumped) {
19931993
// when jump>0, apply the bumps (if any) at the end of the successfully completed jump sample
19941994
ASSERT(jump>0, "jump(%d)>0", jump);
1995-
memcpy(type, tmpType, (size_t)ncol);
1995+
memcpy(type, tmpType, ncol);
19961996
}
19971997
if (verbose && (bumped || jump==0 || jump==nJumps-1)) {
19981998
DTPRINT(_(" Type codes (jump %03d) : %s Quote rule %d\n"), jump, typesAsString(ncol), quoteRule);
@@ -2102,11 +2102,11 @@ int freadMain(freadMainArgs _args) {
21022102
if (verbose) DTPRINT(_(" All rows were sampled since file is small so we know nrow=%"PRIu64" exactly\n"), (uint64_t)sampleLines);
21032103
estnrow = allocnrow = sampleLines;
21042104
} else {
2105-
bytesRead = (size_t)(eof - firstRowStart);
2105+
bytesRead = eof - firstRowStart;
21062106
meanLineLen = (double)sumLen/sampleLines;
21072107
estnrow = CEIL(bytesRead/meanLineLen); // only used for progress meter and verbose line below
21082108
double sd = sqrt( (sumLenSq - (sumLen*sumLen)/sampleLines)/(sampleLines-1) );
2109-
allocnrow = clamp_szt((size_t)(bytesRead / fmax(meanLineLen - 2*sd, minLen)),
2109+
allocnrow = clamp_i64t(bytesRead / fmax(meanLineLen - 2*sd, minLen),
21102110
(size_t)(1.1*estnrow), 2*estnrow);
21112111
// sd can be very close to 0.0 sometimes, so apply a +10% minimum
21122112
// blank lines have length 1 so for fill=true apply a +100% maximum. It'll be grown if needed.
@@ -2187,7 +2187,7 @@ int freadMain(freadMainArgs _args) {
21872187
{
21882188
if (verbose) DTPRINT(_("[09] Apply user overrides on column types\n"));
21892189
ch = pos;
2190-
memcpy(tmpType, type, (size_t)ncol) ;
2190+
memcpy(tmpType, type, ncol) ;
21912191
if (!userOverride(type, colNames, colNamesAnchor, ncol)) { // colNames must not be changed but type[] can be
21922192
if (verbose) DTPRINT(_(" Cancelled by user: userOverride() returned false.")); // # nocov
21932193
freadCleanup(); // # nocov
@@ -2274,7 +2274,7 @@ int freadMain(freadMainArgs _args) {
22742274
nJumps = (int)(bytesRead/chunkBytes);
22752275
if (nJumps==0) nJumps=1;
22762276
else if (nJumps>nth) nJumps = nth*(1+(nJumps-1)/nth);
2277-
chunkBytes = bytesRead / (size_t)nJumps;
2277+
chunkBytes = bytesRead / nJumps;
22782278
} else {
22792279
ASSERT(nJumps==1 /*when nrowLimit supplied*/ || nJumps==2 /*small files*/, "nJumps (%d) != 1|2", nJumps);
22802280
nJumps=1;
@@ -2372,10 +2372,10 @@ int freadMain(freadMainArgs _args) {
23722372
}
23732373
}
23742374

2375-
const char *tch = jump==jump0 ? headPos : nextGoodLine(pos+(size_t)jump*chunkBytes, ncol);
2375+
const char *tch = jump==jump0 ? headPos : nextGoodLine(pos+jump*chunkBytes, ncol);
23762376
const char *thisJumpStart = tch; // "this" for prev/this/next adjective used later, rather than a (mere) t prefix for thread-local.
23772377
const char *tLineStart = tch;
2378-
const char *nextJumpStart = jump<nJumps-1 ? nextGoodLine(pos+(size_t)(jump+1)*chunkBytes, ncol) : eof;
2378+
const char *nextJumpStart = jump<nJumps-1 ? nextGoodLine(pos+(jump+1)*chunkBytes, ncol) : eof;
23792379

23802380
void *targets[9] = {NULL, ctx.buff1, NULL, NULL, ctx.buff4, NULL, NULL, NULL, ctx.buff8};
23812381
FieldParseContext fctx = {

0 commit comments

Comments
 (0)