From c2aa7526ffe5df5db079214f2e65325edd883c02 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sun, 21 Jul 2024 12:42:26 -0700 Subject: [PATCH 1/2] Ensure we're not UNPROTECT()ing 'x' --- src/gsumm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gsumm.c b/src/gsumm.c index 7607f4258..f7504291e 100644 --- a/src/gsumm.c +++ b/src/gsumm.c @@ -594,8 +594,8 @@ 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+=2; } const double *restrict gx = gather(x, &anyNA); ans = PROTECT(allocVector(REALSXP, ngrp)); protecti++; From 5c7527fed68bf75f0542ae94763e2940c26ac350 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Mon, 22 Jul 2024 16:17:01 -0700 Subject: [PATCH 2/2] UNPROTECT()+PROTECT() approach --- src/gsumm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/gsumm.c b/src/gsumm.c index f7504291e..16cc228a3 100644 --- a/src/gsumm.c +++ b/src/gsumm.c @@ -595,7 +595,8 @@ SEXP gmean(SEXP x, SEXP narmArg) case REALSXP: { if (INHERITS(x, char_integer64)) { SEXP as = PROTECT(ScalarReal(1)); - x = PROTECT(coerceAs(x, as, /*copyArg=*/ScalarLogical(TRUE))); protecti+=2; + 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++;