Permalink
Browse files

Stop split_indices from segfaulting. Fixes #104

  • Loading branch information...
1 parent e70081b commit 73c8ed27c4982e2d1704501b89d8b06690e2b5e8 @hadley committed Oct 17, 2012
Showing with 11 additions and 2 deletions.
  1. +1 −0 R/split-indices.r
  2. +7 −0 inst/tests/test-split-indices.r
  3. +3 −2 src/split-numeric.c
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))
}
@@ -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")
+
+})
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

0 comments on commit 73c8ed2

Please sign in to comment.