From 9de7c420c2372770b3b8083abc11b9b0d5b33021 Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Mon, 4 Dec 2023 11:49:55 -0500 Subject: [PATCH] Fix potential crashes in selection commands Check that molecules have atoms before doing something Hat tip to Kiran_Vasave on the forum Signed-off-by: Geoff Hutchison --- avogadro/qtplugins/select/select.cpp | 30 +++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/avogadro/qtplugins/select/select.cpp b/avogadro/qtplugins/select/select.cpp index d4987c1fea..be76ac0fb8 100644 --- a/avogadro/qtplugins/select/select.cpp +++ b/avogadro/qtplugins/select/select.cpp @@ -139,7 +139,7 @@ bool Select::evalSelect(bool input, Index index) const void Select::selectAll() { - if (m_molecule) { + if (m_molecule && m_molecule->atomCount() > 0) { for (Index i = 0; i < m_molecule->atomCount(); ++i) { m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i)); } @@ -150,7 +150,7 @@ void Select::selectAll() void Select::selectNone() { - if (m_molecule) { + if (m_molecule && m_molecule->atomCount() > 0) { for (Index i = 0; i < m_molecule->atomCount(); ++i) m_molecule->undoMolecule()->setAtomSelected(i, false); @@ -160,7 +160,7 @@ void Select::selectNone() void Select::selectElement() { - if (!m_molecule) + if (m_molecule == nullptr || m_molecule->atomCount() == 0) return; if (m_elements == nullptr) { @@ -174,7 +174,7 @@ void Select::selectElement() void Select::selectElement(int element) { - if (!m_molecule) + if (m_molecule == nullptr || m_molecule->atomCount() == 0) return; QString undoText = tr("Select Element"); @@ -211,7 +211,7 @@ bool Select::isWaterOxygen(Index i) void Select::selectWater() { - if (!m_molecule) + if (m_molecule == nullptr || m_molecule->atomCount() == 0) return; QString undoText = tr("Select Water"); @@ -260,6 +260,9 @@ void Select::selectWater() void Select::selectBackboneAtoms() { + if (m_molecule == nullptr || m_molecule->atomCount() == 0) + return; + // unselect everything selectNone(); @@ -296,6 +299,9 @@ void Select::selectBackboneAtoms() void Select::selectSidechainAtoms() { + if (m_molecule == nullptr || m_molecule->atomCount() == 0) + return; + // unselect everything selectNone(); @@ -350,6 +356,9 @@ Vector3 Select::getSelectionCenter() void Select::enlargeSelection() { + if (m_molecule == nullptr || m_molecule->atomCount() == 0) + return; + Vector3 center = getSelectionCenter(); // find the current max distance of the selection Real maxDistance = 0.0; @@ -384,6 +393,9 @@ void Select::enlargeSelection() void Select::shrinkSelection() { + if (m_molecule == nullptr || m_molecule->atomCount() == 0) + return; + Vector3 center = getSelectionCenter(); // find the current max distance of the selection Real maxDistance = 0.0; @@ -420,7 +432,7 @@ void Select::shrinkSelection() void Select::selectAtomIndex() { - if (!m_molecule) + if (m_molecule == nullptr || m_molecule->atomCount() == 0) return; bool ok; @@ -461,7 +473,7 @@ void Select::selectAtomIndex() void Select::selectResidue() { - if (!m_molecule) + if (m_molecule == nullptr || m_molecule->atomCount() == 0) return; bool ok; @@ -531,7 +543,7 @@ void Select::selectResidue() void Select::invertSelection() { - if (m_molecule) { + if (m_molecule && m_molecule->atomCount() > 0) { for (Index i = 0; i < m_molecule->atomCount(); ++i) m_molecule->undoMolecule()->setAtomSelected( i, evalSelect(!m_molecule->atomSelected(i), i), tr("Invert Selection")); @@ -541,7 +553,7 @@ void Select::invertSelection() void Select::createLayerFromSelection() { - if (!m_molecule) + if (m_molecule == nullptr || m_molecule->atomCount() == 0) return; QtGui::RWMolecule* rwmol = m_molecule->undoMolecule();