From 97683c01ecc1fd6d4ec9927fbb0bc966f53a1335 Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Sat, 7 Sep 2024 17:36:02 -0400 Subject: [PATCH] Add double-check for addEdge to prevent potential crash Fixes bug from @aeonik https://discuss.avogadro.cc/t/crash-when-dragging-atom-over-other-atom-to-create-bond/4964/18 Signed-off-by: Geoff Hutchison --- avogadro/core/graph.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/avogadro/core/graph.cpp b/avogadro/core/graph.cpp index e6faa8e61..1757c0dce 100644 --- a/avogadro/core/graph.cpp +++ b/avogadro/core/graph.cpp @@ -224,7 +224,10 @@ size_t Graph::addEdge(size_t a, size_t b) m_subgraphDirty[subgraphA] || m_subgraphDirty[subgraphB]; for (size_t i : m_subgraphToVertices[subgraphB]) { m_subgraphToVertices[subgraphA].insert(i); - m_vertexToSubgraph[i] = subgraphA; + if (i < m_vertexToSubgraph.size()) + m_vertexToSubgraph[i] = subgraphA; + else + m_vertexToSubgraph.push_back(subgraphA); } // Just leave it empty, it could be reused m_subgraphToVertices[subgraphB].clear(); @@ -433,8 +436,7 @@ size_t Graph::edgeCount() const std::vector Graph::neighbors(size_t index) const { - if(index==size()) - { + if (index == size()) { std::vector emptyVector; return std::vector(emptyVector); }