From b9b17b756639fd20f31455a96fc14b254be19196 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Wed, 28 May 2025 11:48:30 +0200 Subject: [PATCH 1/2] Fix out of bounds erase The intention is to remove a cluster that was just created, but remove will end up calling std::vector::erase for the element that is after the last one. The definition is here: https://github.com/iLCSoft/ConformalTracking/blob/master/include/KDTrack.h#L37 or basically ``` void remove(int clusterN) { m_clusters.erase(m_clusters.begin() + clusterN); } ``` --- src/ConformalTracking.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConformalTracking.cc b/src/ConformalTracking.cc index 26aca9b..36ecff0 100644 --- a/src/ConformalTracking.cc +++ b/src/ConformalTracking.cc @@ -2049,7 +2049,7 @@ void ConformalTracking::extendTracksPerLayer(UniqueKDTracks& conformalTracks, Sh deltaChi2zs = newchi2zs - chi2zs; streamlog_out(DEBUG9) << "-- hit was fitted and has a delta chi2 of " << deltaChi2 << " and delta chi2zs of " << deltaChi2zs << std::endl; - tempTrack.remove(tempTrack.m_clusters.size()); + tempTrack.remove(tempTrack.m_clusters.size() - 1); streamlog_out(DEBUG9) << "-- tempTrack has now " << tempTrack.m_clusters.size() << " hits " << std::endl; double chi2cut = parameters._chi2cut; From 609cc1de9964c07508f1c0e0864517866276e6cf Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Wed, 28 May 2025 12:44:49 +0200 Subject: [PATCH 2/2] Add an exception when removing out of bounds --- include/KDTrack.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/KDTrack.h b/include/KDTrack.h index 7e89f5d..5cc8fdc 100644 --- a/include/KDTrack.h +++ b/include/KDTrack.h @@ -5,6 +5,7 @@ #include "Parameters.h" #include +#include class TH2F; @@ -35,6 +36,9 @@ class KDTrack { m_nPoints++; } void remove(int clusterN) { + if(clusterN < 0 || clusterN >= int(m_clusters.size())) { + throw std::out_of_range("KDTrack::remove: clusterN out of range"); + } m_clusters.erase(m_clusters.begin() + clusterN); m_nPoints--; }