Skip to content

Commit

Permalink
Merge pull request #1097 from lionel-/fix-cast
Browse files Browse the repository at this point in the history
Fix UBSAN error
  • Loading branch information
lionel- authored May 9, 2020
2 parents 5b71d88 + a33d116 commit 570a00c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
3 changes: 1 addition & 2 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

* The UBSAN error is fixed.
* The r-devel warning is fixed.
This is a resubmission to fix the SAN error.


## Test environments
Expand Down
14 changes: 9 additions & 5 deletions src/subscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,21 @@ static SEXP dbl_cast_subscript(SEXP subscript,
continue;
}

int out_elt = (int) elt;

// Detect out-of-bounds and fractional numbers
if (elt > INT_MAX || out_elt != elt) {
if (!isfinite(elt) || elt <= INT_MIN || elt > INT_MAX) {
// Once we throw lazy errors from the cast method, we should
// throw the error here as well
UNPROTECT(1);
return dbl_cast_subscript_fallback(subscript, opts, err);
}

out_p[i] = out_elt;
int elt_int = (int) elt;

if (elt != elt_int) {
UNPROTECT(1);
return dbl_cast_subscript_fallback(subscript, opts, err);
}

out_p[i] = elt_int;
}

UNPROTECT(1);
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/error/test-subscript-loc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ i It must be numeric or character.
Error: Must extract element with a single valid subscript.
x Can't convert from <double> to <integer> due to loss of precision.

> vec_as_location2(Inf, 10L)
Error: Must extract element with a single valid subscript.
x Can't convert from <double> to <integer> due to loss of precision.

> vec_as_location2(-Inf, 10L)
Error: Must extract element with a single valid subscript.
x Can't convert from <double> to <integer> due to loss of precision.

> # Idem with custom `arg`
> vec_as_location2(foobar(), 10L, arg = "foo")
Error: Must extract element with a single valid subscript.
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-subscript-loc.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ test_that("vec_as_location2() requires integer or character inputs", {
expect_error(vec_as_location2(env(), 10L), class = "vctrs_error_subscript_type")
expect_error(vec_as_location2(foobar(), 10L), class = "vctrs_error_subscript_type")
expect_error(vec_as_location2(2.5, 10L), class = "vctrs_error_subscript_type")
expect_error(vec_as_location2(Inf, 10L), class = "vctrs_error_subscript_type")
expect_error(vec_as_location2(-Inf, 10L), class = "vctrs_error_subscript_type")

"Idem with custom `arg`"
expect_error(vec_as_location2(foobar(), 10L, arg = "foo"), class = "vctrs_error_subscript_type")
Expand Down Expand Up @@ -481,6 +483,8 @@ test_that("conversion to locations has informative error messages", {
vec_as_location2(env(), 10L)
vec_as_location2(foobar(), 10L)
vec_as_location2(2.5, 3L)
vec_as_location2(Inf, 10L)
vec_as_location2(-Inf, 10L)
"Idem with custom `arg`"
vec_as_location2(foobar(), 10L, arg = "foo")
vec_as_location2(2.5, 3L, arg = "foo")
Expand Down

0 comments on commit 570a00c

Please sign in to comment.