Skip to content

Commit 5e4b78c

Browse files
author
ripley
committed
avoid illegal accesses with qsort on a 0-length input
git-svn-id: https://svn.r-project.org/R/trunk@87432 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent d283c17 commit 5e4b78c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

doc/NEWS.Rd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,9 @@
469469
470470
\item \code{isGeneric(fdef = print)} now works, fixing \PR{18369}
471471
thanks to \I{Mikael Jagan}.
472+
473+
\item \code{sort(x, method = "qsort")} made illegal accesses when
474+
\code{x} has length 0.
472475
}
473476
}
474477
}

src/main/qsort.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,23 @@ attribute_hidden SEXP do_qsort(SEXP call, SEXP op, SEXP args, SEXP rho)
7474
PROTECT(indx = allocVector(REALSXP, n));
7575
double *ix = REAL(indx);
7676
for(R_xlen_t i = 0; i < n; i++) ix[i] = (double) (i+1);
77-
if(x_int) R_qsort_int_R(ivx, ix, 1, n);
78-
else R_qsort_R(vx, ix, 1, n);
77+
// do not need to sort 0-length array
78+
if (n > 0) {
79+
if(x_int) R_qsort_int_R(ivx, ix, 1, n);
80+
else R_qsort_R(vx, ix, 1, n);
81+
}
7982
} else
8083
#endif
8184
{
8285
PROTECT(indx = allocVector(INTSXP, n));
8386
int *ix = INTEGER(indx);
8487
int nn = (int) n;
8588
for(int i = 0; i < nn; i++) ix[i] = i+1;
86-
if(x_int) R_qsort_int_I(ivx, ix, 1, nn);
87-
else R_qsort_I(vx, ix, 1, nn);
89+
// do not need to sort 0-length array
90+
if (nn > 0) {
91+
if(x_int) R_qsort_int_I(ivx, ix, 1, nn);
92+
else R_qsort_I(vx, ix, 1, nn);
93+
}
8894
}
8995

9096
SET_VECTOR_ELT(ans, 0, sx);

0 commit comments

Comments
 (0)