Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Stop split_indices from segfaulting. Fixes #104

  • Loading branch information...
commit 73c8ed27c4982e2d1704501b89d8b06690e2b5e8 1 parent e70081b
Hadley Wickham authored
1  R/split-indices.r
View
@@ -11,6 +11,7 @@
split_indices <- function(group, n = max(group)) {
if (length(group) == 0) return(integer())
stopifnot(is.integer(group))
+ n <- as.integer(n)
.Call("split_indices", group, as.integer(n))
}
7 inst/tests/test-split-indices.r
View
@@ -0,0 +1,7 @@
+context("Split indices")
+
+test_that("Error if n too small", {
+ expect_error(split_indices(1:10, 5),
+ "n smaller than largest index")
+
+})
5 src/split-numeric.c
View
@@ -14,8 +14,9 @@ SEXP split_indices(SEXP group, SEXP n) {
for (i = 0; i < nlevs; i++)
counts[i] = 0;
for (i = 0; i < nobs; i++) {
- j = pgroup[i] - 1;
- counts[j]++;
+ j = pgroup[i];
+ if (j > nlevs) error("n smaller than largest index");
+ counts[j - 1]++;
}
// Allocate storage for results
Please sign in to comment.
Something went wrong with that request. Please try again.