From 28be49bfd9369a74019a4588258ff2a4b35366b4 Mon Sep 17 00:00:00 2001 From: Tom Andrews Date: Mon, 6 Aug 2018 18:47:09 +0100 Subject: [PATCH] Fix illegal read in do_is_ordered do_is_ordered tries to check whether the first element of the series is NA. It needs to first check whether the series has at least one element to avoid reading an uninitialised value. Fixes #236. --- src/isOrdered.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/isOrdered.c b/src/isOrdered.c index b6ee86ab..3e07a603 100644 --- a/src/isOrdered.c +++ b/src/isOrdered.c @@ -31,6 +31,12 @@ SEXP do_is_ordered (SEXP x, SEXP increasing, SEXP strictly) double *real_x; int *int_x; + /* + If length is 0 then it is ordered + */ + if (nx < 0) + return ScalarLogical(1); + if(TYPEOF(x) == REALSXP) { /* Check for increasing order, strict or non-strict @@ -76,7 +82,7 @@ SEXP do_is_ordered (SEXP x, SEXP increasing, SEXP strictly) */ int_x = INTEGER(x); if(LOGICAL(increasing)[ 0 ] == 1) { /* INCREASING */ - /* Not increasing order if first element is NA */ + /* Not increasing order if first element is NA. We know x has at least 1 element. */ if( int_x[0] == NA_INTEGER ) return ScalarLogical(0);