From 6ede028b39a021636ae44873e13b93859d957e80 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Fri, 11 Nov 2022 20:50:36 -0600 Subject: [PATCH] Prevent out-of-bounds memory access on Compton profile data --- src/photon.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/photon.cpp b/src/photon.cpp index 2f9bc5e3dd..a500ff2d4c 100644 --- a/src/photon.cpp +++ b/src/photon.cpp @@ -226,8 +226,9 @@ PhotonInteraction::PhotonInteraction(hid_t group) // Create Compton profile CDF auto n_profile = data::compton_profile_pz.size(); - profile_cdf_ = xt::empty({n_shell, n_profile}); - for (int i = 0; i < profile_pdf_.shape(0); ++i) { + auto n_shell_compton = profile_pdf_.shape(0); + profile_cdf_ = xt::empty({n_shell_compton, n_profile}); + for (int i = 0; i < n_shell_compton; ++i) { double c = 0.0; profile_cdf_(i, 0) = 0.0; for (int j = 0; j < n_profile - 1; ++j) { @@ -409,6 +410,13 @@ void PhotonInteraction::compton_scatter(double alpha, bool doppler, double E_out; this->compton_doppler(alpha, *mu, &E_out, i_shell, seed); *alpha_out = E_out / MASS_ELECTRON_EV; + + // It's possible for the Compton profile data to have more shells than + // there are in the ENDF data. Make sure the shell index doesn't end up + // out of bounds. + if (*i_shell >= shells_.size()) { + *i_shell = -1; + } } else { *i_shell = -1; }