Skip to content

Commit e201bb9

Browse files
committed
between: check for integer64 before coercion
In particular, avoid calling fitsInInt32() on REALSXP vectors that could possibly be of class 'integer64' (and therefore shouldn't be read as doubles).
1 parent 380bcba commit e201bb9

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/between.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP incbounds, SEXP NAboundsArg, S
2929
const bool check = LOGICAL(checkArg)[0];
3030
const bool verbose = GetVerbose();
3131

32+
// check before potential coercion which ignores methods, #7164
33+
if (INHERITS(x, char_integer64)) {
34+
if (!INHERITS(lower, char_integer64) || !INHERITS(upper, char_integer64))
35+
error(_("x is integer64 but lower and/or upper are not.")); // e.g. between(int64, character, character)
36+
} else if (INHERITS(lower, char_integer64) || INHERITS(upper, char_integer64))
37+
error(_("x is not integer64 but lower and/or upper is integer64. Please align classes."));
38+
3239
if (isInteger(x)) {
3340
if ((isInteger(lower) || fitsInInt32(lower)) &&
3441
(isInteger(upper) || fitsInInt32(upper))) { // #3517 coerce to num to int when possible
@@ -90,8 +97,6 @@ SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP incbounds, SEXP NAboundsArg, S
9097

9198
case REALSXP:
9299
if (INHERITS(x, char_integer64)) {
93-
if (!INHERITS(lower, char_integer64) || !INHERITS(upper, char_integer64))
94-
error(_("x is integer64 but lower and/or upper are not.")); // e.g. between(int64, character, character)
95100
const int64_t *lp = (int64_t *)REAL(lower);
96101
const int64_t *up = (int64_t *)REAL(upper);
97102
const int64_t *xp = (int64_t *)REAL(x);
@@ -117,8 +122,6 @@ SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP incbounds, SEXP NAboundsArg, S
117122
}
118123
if (verbose) Rprintf(_("between parallel processing of integer64 took %8.3fs\n"), omp_get_wtime()-tic);
119124
} else {
120-
if (INHERITS(lower, char_integer64) || INHERITS(upper, char_integer64))
121-
error(_("x is not integer64 but lower and/or upper is integer64. Please align classes."));
122125
const double *lp = REAL(lower);
123126
const double *up = REAL(upper);
124127
const double *xp = REAL(x);

0 commit comments

Comments
 (0)