mrc-ide / odin

ᚩ A DSL for describing and solving differential equations in R
https://mrc-ide.github.io/odin
Other
106 stars 13 forks source link

Providing defaults to array variables compiles but crashes #277

Open richfitz opened 2 years ago

richfitz commented 2 years ago
odin::odin({
  X[] <- user(1)
  dim(X) <- 5
  initial(a) <- 0
  deriv(a) <- sum(X)
}, workdir = "err")

then look in err/src/odin.c

SEXP odin_create(SEXP user) {
  odin_internal *internal = (odin_internal*) R_Calloc(1, odin_internal);
  internal->X = NULL;
  internal->dim_X = 5;
  internal->initial_a = 0;
  internal->X = 1;
  SEXP ptr = PROTECT(R_MakeExternalPtr(internal, R_NilValue, R_NilValue));
  R_RegisterCFinalizer(ptr, odin_finalise);
  UNPROTECT(1);
  return ptr;
}

which creates a really bad pointer for X (and later on a crash). We should either support this properly, or error

Reported by @lwhittles