From fe2c7aa7ac3d9fca145e3c9d646679db778822ce Mon Sep 17 00:00:00 2001
From: "Pavel N. Krivitsky" <p.krivitsky@unsw.edu.au>
Date: Mon, 4 Nov 2024 21:01:11 +1100
Subject: [PATCH] setListElement() now PROTECT()s the value being stored in the
 list first thing, then unprotects it once it has been stored.

---
 inst/include/ergm_Rutil.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/inst/include/ergm_Rutil.h b/inst/include/ergm_Rutil.h
index 24e6fe302..2f62e7128 100644
--- a/inst/include/ergm_Rutil.h
+++ b/inst/include/ergm_Rutil.h
@@ -30,6 +30,7 @@ static inline SEXP getListElement(SEXP list, const char *str){
 }
 
 static inline SEXP setListElement(SEXP list, const char *str, SEXP value){
+  value = PROTECT(value);
   SEXP names = getAttrib(list, R_NamesSymbol);
 
   for (unsigned int i = 0; i < length(list); i++)
@@ -37,6 +38,7 @@ static inline SEXP setListElement(SEXP list, const char *str, SEXP value){
       SET_VECTOR_ELT(list, i, value);
       return value;
     }
+  UNPROTECT(1);
   error("List does not have element '%s' to set.", str);
   return R_NilValue;
 }