[Rd] Forcing a PROTECT Bug to Occur

Michael Milton ttm|gue|tt @end|ng |rom gm@||@com
Sat Apr 29 19:26:18 CEST 2023


I'm trying to learn about R's PROTECT system. To that end I've tried to
create an example of C code that doesn't protect anything. I was hoping it
would either segfault from trying to access deallocated memory, or maybe
print out nonsense results because the unprotected memory got overwritten,
but I can't make either happen.

Here's my current code (all in R, using the inline package for simplicity):

> gctorture(TRUE)
> z = inline::cfunction(body="
    SEXP vec_1 = Rf_ScalarInteger(99);
    SEXP vec_2 = Rf_allocVector(VECSXP, 10);
    SET_VECTOR_ELT(vec_2, 1, vec_1);
    Rf_PrintValue(vec_2);
")

My thinking was that, with torture mode enabled, the allocation of vec_2
should ensure that vec_1 is collected, and then trying to put it into vec_2
and then print it would then fail. But it consistently prints 99 in the
list's second element.

Why does this code not have issues? Alternatively, is there a simpler
example of C code that demonstrates missing PROTECT calls?

	[[alternative HTML version deleted]]



More information about the R-devel mailing list