stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
140 stars 44 forks source link

[BUG] `transform_inits` does not check validity of var_context #1315

Closed WardBrian closed 1 year ago

WardBrian commented 1 year ago

Current Behavior:

See https://github.com/stan-dev/stan/issues/3182.

In short, transform_inits will happily read in a var_context object which is the wrong size. For example:

parameters {
  array[0] real a;
  array[1] real b;
}
{ "a":  2.1223971837869353 , "b": [ 5.1902179174507062 ] }

After #1305, this will read in a value for a even though one isn't expected, causing the stan::io::serializer to run out of room when trying to write the value for b, yielding an error like

Unrecoverable error evaluating the log probability at the initial value.
Exception: In serializer: Storage capacity [1] exceeded while writing value of size [1] from position [1]. This is an internal error, if you see it please report it as an issue on the Stan github repository. (found before start of program)

Before #1305, a different kind of error could happen:

parameters {
  array[1] real a;
}
{ "a": [] }

Running this with a compiler from before #1305 results in a segmentation fault, after it just doesn't read anything for a.

Expected Behavior:

An error when the transform_inits var context has data of the wrong size (or, possibly, wrong shape? it seems a lot of R code is relying on us not being too picky about the shapes here...)