From b069b688c0d4aa5e4c54402dd5720903474f8288 Mon Sep 17 00:00:00 2001 From: "Daniel R. Roe" Date: Wed, 30 Sep 2026 08:40:22 -0400 Subject: [PATCH 1/2] Avoid integer overflow in LJ VDW long range correction prefactor calc by converting # VDW atoms with type var to double before multiplying. --- src/Energy/VDW_LongRange_Correction.cpp | 15 +++++++++++++-- src/Energy/VDW_LongRange_Correction.h | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Energy/VDW_LongRange_Correction.cpp b/src/Energy/VDW_LongRange_Correction.cpp index 1e69c7beb6..13de574b3a 100644 --- a/src/Energy/VDW_LongRange_Correction.cpp +++ b/src/Energy/VDW_LongRange_Correction.cpp @@ -1,6 +1,7 @@ #include "VDW_LongRange_Correction.h" #include "../CpptrajStdio.h" #include "../Topology.h" +#include using namespace Cpptraj::Energy; @@ -48,9 +49,19 @@ int VDW_LongRange_Correction::Setup_VDW_Correction(Topology const& topIn, unsigned int idx = offset + jtype; int nbidx = NB_->NBindex()[ idx ]; if (nbidx > -1) { - atype_vdw_term += N_vdw_type_[itype] * N_vdw_type_[jtype] * NB_->NBarray()[ nbidx ].B(); + // Avoid integer overflow by converting to double. + double Ni = (double)N_vdw_type_[itype]; + double Nj = (double)N_vdw_type_[jtype]; + double NiNj = Ni * Nj; + // Sanity check + if (std::isinf(NiNj)) { + mprinterr("Internal Error: overflow detected calculating LJ VDW LR correction (%i types=%i, %i types=%i)\n", + itype, N_vdw_type_[itype], jtype, N_vdw_type_[jtype]); + return 1; + } + atype_vdw_term += NiNj * NB_->NBarray()[ nbidx ].B(); - Vdw_Recip_term_ += N_vdw_type_[itype] * N_vdw_type_[jtype] * NB_->NBarray()[ nbidx ].B(); + Vdw_Recip_term_ += NiNj * NB_->NBarray()[ nbidx ].B(); } } atype_vdw_recip_terms_.push_back(atype_vdw_term); diff --git a/src/Energy/VDW_LongRange_Correction.h b/src/Energy/VDW_LongRange_Correction.h index ac10195406..fad4642ccb 100644 --- a/src/Energy/VDW_LongRange_Correction.h +++ b/src/Energy/VDW_LongRange_Correction.h @@ -32,7 +32,7 @@ class VDW_LongRange_Correction { double Vdw_Recip_term_; int debug_; // Below variables are needed for per-atom decomp - Iarray vdw_type_; ///< VDW type for each seleccted atom (#atoms) + Iarray vdw_type_; ///< VDW type for each selected atom (#atoms) Iarray N_vdw_type_; ///< Count of atoms that have each VDW type index (#types) Darray atype_vdw_recip_terms_; ///< the nonbond interaction for each atom type (#types) }; From eacd9b6049cf564f8445e0183e5fb9ff6ed1b121 Mon Sep 17 00:00:00 2001 From: "Daniel R. Roe" Date: Wed, 30 Sep 2026 08:44:04 -0400 Subject: [PATCH 2/2] 7.12.0. Minor version bump for fixed potential VDW LR correction overflow --- src/Version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Version.h b/src/Version.h index cf1a20f342..96e636a0a6 100644 --- a/src/Version.h +++ b/src/Version.h @@ -12,7 +12,7 @@ * Whenever a number that precedes is incremented, all subsequent * numbers should be reset to 0. */ -#define CPPTRAJ_INTERNAL_VERSION "V7.11.2" +#define CPPTRAJ_INTERNAL_VERSION "V7.12.0" /// PYTRAJ relies on this #define CPPTRAJ_VERSION_STRING CPPTRAJ_INTERNAL_VERSION #endif