Skip to content

Commit c997e3b

Browse files
authored
Merge pull request #3364 from stan-dev/pathfinder-oob-read
Avoid indexing past vector length in Pathfinder writer
2 parents 07585a9 + f942abc commit c997e3b

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/stan/services/pathfinder/multi.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,13 @@ inline int pathfinder_lbfgs_multi(
343343
sample_row.tail(uc_param_size) = approx_samples_constrained_col;
344344
safe_write(sample_row);
345345
// If we see the same draw idx more than once, just increment j and
346-
// write again
347-
while (j < (single_path_psis_idxs[i].second)
348-
&& draw_idx == psis_draw_idxs.coeff(j + 1)) {
349-
safe_write(sample_row);
350-
++j;
346+
// write again if there are remaining draws
347+
if (j < (psis_draw_idxs.size() - 1)) {
348+
while (j < (single_path_psis_idxs[i].second)
349+
&& draw_idx == psis_draw_idxs.coeff(j + 1)) {
350+
safe_write(sample_row);
351+
++j;
352+
}
351353
}
352354
}
353355
}

0 commit comments

Comments
 (0)