Skip to content

Commit d0887df

Browse files
fread(): avoid casting int* to Rboolean* (#6782)
* fread(): avoid casting int* to Rboolean* This used to work because the compilers were very likely to use int as the underlying type of the enum. R-devel r87656 changes the definition of Rboolean from an enum to bool, sothe sizes and the semantics of the types are very different now. Since LOGICAL() returns int*, and NA_INTEGER is an int, keep using that to access the vector contents. * safe change in %notin% * safe change sizeof() * Local logical scalars LOGICAL(x)[0] --> int, not Rboolean * One more sizeof treatment --------- Co-authored-by: Michael Chirico <[email protected]> Co-authored-by: Michael Chirico <[email protected]>
1 parent 874e571 commit d0887df

File tree

7 files changed

+9
-9
lines changed

7 files changed

+9
-9
lines changed

src/assign.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ const char *memrecycle(const SEXP target, const SEXP where, const int start, con
10611061
case RAWSXP: BODY(Rbyte, RAW, int, val!=0, td[i]=cval)
10621062
case LGLSXP:
10631063
if (mc) {
1064-
memcpy(td, LOGICAL(source), slen*sizeof(Rboolean)); break;
1064+
memcpy(td, LOGICAL(source), slen*sizeof(int)); break;
10651065
} else BODY(int, LOGICAL, int, val, td[i]=cval)
10661066
case INTSXP: BODY(int, INTEGER, int, val==NA_INTEGER ? NA_LOGICAL : val!=0, td[i]=cval)
10671067
case REALSXP:
@@ -1207,7 +1207,7 @@ void writeNA(SEXP v, const int from, const int n, const bool listNA)
12071207
memset(RAW(v)+from, 0, n*sizeof(Rbyte));
12081208
break;
12091209
case LGLSXP: {
1210-
Rboolean *vd = (Rboolean *)LOGICAL(v);
1210+
int *vd = (int *)LOGICAL(v);
12111211
for (int i=from; i<=to; ++i) vd[i] = NA_LOGICAL;
12121212
} break;
12131213
case INTSXP: {

src/freadR.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ void pushBuffer(ThreadLocalFreadParsingContext *ctx)
644644
} else
645645
if (thisSize == 1) {
646646
if (type[j] > CT_BOOL8_Y) STOP(_("Field size is 1 but the field is of type %d\n"), type[j]);
647-
Rboolean *dest = (Rboolean *)LOGICAL(VECTOR_ELT(DT, resj)) + DTi;
647+
int *dest = LOGICAL(VECTOR_ELT(DT, resj)) + DTi;
648648
const char *src1 = (char*)buff1 + off1;
649649
for (int i=0; i<nRows; ++i) {
650650
int8_t v = *(int8_t *)src1;

src/fsort.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ SEXP fsort(SEXP x, SEXP verboseArg) {
117117
t[0] = wallclock();
118118
if (!IS_TRUE_OR_FALSE(verboseArg))
119119
error(_("%s must be TRUE or FALSE"), "verbose");
120-
Rboolean verbose = LOGICAL(verboseArg)[0];
120+
int verbose = LOGICAL(verboseArg)[0];
121121
if (!isNumeric(x)) error(_("x must be a vector of type double currently"));
122122
// TODO: not only detect if already sorted, but if it is, just return x to save the duplicate
123123

src/negate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ void negateByRef(SEXP x) {
55
error("not logical or integer vector"); // # nocov
66
}
77
const int n = length(x);
8-
Rboolean *ansd = (Rboolean *)LOGICAL(x);
8+
int *ansd = (int *)LOGICAL(x);
99
for(int i=0; i<n; ++i) {
1010
ansd[i] ^= (ansd[i] != NA_LOGICAL); // invert true/false but leave NA alone
1111
}

src/rbindlist.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg, SEXP ignor
1212
error(_("%s should be TRUE or FALSE"), "ignore.attr");
1313
if (!length(l)) return(l);
1414
if (TYPEOF(l) != VECSXP) error(_("Input to rbindlist must be a list. This list can contain data.tables, data.frames or plain lists."));
15-
Rboolean usenames = LOGICAL(usenamesArg)[0];
15+
int usenames = LOGICAL(usenamesArg)[0];
1616
const bool fill = LOGICAL(fillArg)[0];
1717
const bool ignoreattr = LOGICAL(ignoreattrArg)[0];
1818
if (fill && usenames==NA_LOGICAL) {

src/uniqlist.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,13 @@ SEXP uniqueNlogical(SEXP x, SEXP narmArg) {
356356
const R_xlen_t n = xlength(x);
357357
if (n==0)
358358
return ScalarInteger(0); // empty vector
359-
Rboolean first = LOGICAL(x)[0];
359+
int first = LOGICAL(x)[0];
360360
R_xlen_t i=0;
361361
const int *ix = LOGICAL(x);
362362
while (++i<n && ix[i]==first);
363363
if (i==n)
364364
return ScalarInteger(first==NA_INTEGER && narm ? 0 : 1); // all one value
365-
Rboolean second = ix[i];
365+
int second = ix[i];
366366
// we've found 2 different values (first and second). Which one didn't we find? Then just look for that.
367367
// NA_LOGICAL == INT_MIN checked in init.c
368368
const int third = (first+second == 1) ? NA_LOGICAL : ( first+second == INT_MIN ? TRUE : FALSE );

src/utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ SEXP copyAsPlain(SEXP x) {
227227
memcpy(RAW(ans), RAW(x), n*sizeof(Rbyte));
228228
break;
229229
case LGLSXP:
230-
memcpy(LOGICAL(ans), LOGICAL(x), n*sizeof(Rboolean));
230+
memcpy(LOGICAL(ans), LOGICAL(x), n*sizeof(int));
231231
break;
232232
case INTSXP:
233233
memcpy(INTEGER(ans), INTEGER(x), n*sizeof(int)); // covered by 10:1 after test 178

0 commit comments

Comments
 (0)