diff --git a/avogadro/qtplugins/select/select.cpp b/avogadro/qtplugins/select/select.cpp index be76ac0fb8..a02e17c61d 100644 --- a/avogadro/qtplugins/select/select.cpp +++ b/avogadro/qtplugins/select/select.cpp @@ -450,21 +450,25 @@ void Select::selectAtomIndex() // check if it's a range if (item.contains('-')) { auto range = item.split('-'); - if (range.size() >= 2) { + if (range.size() == 2) { bool ok1, ok2; + int k = m_molecule->atomCount(); int start = range.first().toInt(&ok1); int last = range.back().toInt(&ok2); if (ok1 && ok2) { - for (int i = start; i <= last; ++i) - m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i), - undoText); + if (start < k) + for (int i = start; i <= last; ++i) + m_molecule->undoMolecule()->setAtomSelected( + i, evalSelect(true, i), undoText); } } } else { int i = item.toInt(&ok); + int k = m_molecule->atomCount(); if (ok) - m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i), - undoText); + if (i < k) + m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i), + undoText); } }