Skip to content

Commit

Permalink
Check index length before checking for NA/NaN/Inf
Browse files Browse the repository at this point in the history
This could cause a segfault when the index length is zero.
  • Loading branch information
joshuaulrich committed Oct 12, 2024
1 parent 0b0989d commit 2113fae
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,9 @@ SEXP do_merge_xts (SEXP x, SEXP y,

/* Check for illegal values before looping. Due to ordered index,
* -Inf must be first, while NA, Inf, and NaN must be last. */
if (!R_FINITE(real_xindex[0]) || !R_FINITE(real_xindex[nrx-1]) ||
!R_FINITE(real_yindex[0]) || !R_FINITE(real_yindex[nry-1])) {
int bad_x_index = nrx > 0 && (!R_FINITE(real_xindex[0]) || !R_FINITE(real_xindex[nrx-1]));
int bad_y_index = nry > 0 && (!R_FINITE(real_yindex[0]) || !R_FINITE(real_yindex[nry-1]));
if (bad_x_index || bad_y_index) {
error("'index' cannot contain 'NA', 'NaN', or '+/-Inf'");
}

Expand Down Expand Up @@ -450,7 +451,9 @@ SEXP do_merge_xts (SEXP x, SEXP y,
* results. Note that the NA_integer_ will appear in the last value of
* the index because of sorting at the R level, even though NA_INTEGER
* equals INT_MIN at the C level. */
if (int_xindex[nrx-1] == NA_INTEGER || int_yindex[nry-1] == NA_INTEGER) {
int bad_x_index = nrx > 0 && int_xindex[nrx-1] == NA_INTEGER;
int bad_y_index = nry > 0 && int_yindex[nry-1] == NA_INTEGER;
if (bad_x_index || bad_y_index) {
error("'index' cannot contain 'NA'");
}

Expand Down

0 comments on commit 2113fae

Please sign in to comment.