Skip to content

Commit 2113fae

Browse files
committed
Check index length before checking for NA/NaN/Inf
This could cause a segfault when the index length is zero.
1 parent 0b0989d commit 2113fae

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/merge.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,9 @@ SEXP do_merge_xts (SEXP x, SEXP y,
409409

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

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

0 commit comments

Comments
 (0)