Skip to content

Commit

Permalink
Ensure we're not UNPROTECT()ing 'x' in gsumm (#6306)
Browse files Browse the repository at this point in the history
* Ensure we're not UNPROTECT()ing 'x'

* UNPROTECT()+PROTECT() approach

---------

Co-authored-by: Benjamin Schwendinger <52290390+ben-schwen@users.noreply.github.com>
  • Loading branch information
MichaelChirico and ben-schwen authored Jul 25, 2024
1 parent 4c5785b commit 1600b51
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/gsumm.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,9 @@ SEXP gmean(SEXP x, SEXP narmArg)
x = PROTECT(coerceVector(x, REALSXP)); protecti++;
case REALSXP: {
if (INHERITS(x, char_integer64)) {
x = PROTECT(coerceAs(x, /*as=*/PROTECT(ScalarReal(1)), /*copyArg=*/ScalarLogical(TRUE))); protecti++;
UNPROTECT(1); // as= input to coerceAs()
SEXP as = PROTECT(ScalarReal(1));
x = PROTECT(coerceAs(x, as, /*copyArg=*/ScalarLogical(TRUE))); protecti++;
UNPROTECT(2); PROTECT(x); // PROTECT() is stack-based, UNPROTECT() back to 'as' then PROTECT() 'x' again
}
const double *restrict gx = gather(x, &anyNA);
ans = PROTECT(allocVector(REALSXP, ngrp)); protecti++;
Expand Down

0 comments on commit 1600b51

Please sign in to comment.