diff --git a/External/INCHI-API/inchi.cpp b/External/INCHI-API/inchi.cpp index 6a1bf9d09..5cfaa4208 100644 --- a/External/INCHI-API/inchi.cpp +++ b/External/INCHI-API/inchi.cpp @@ -952,6 +952,7 @@ bool _Valence7SCleanUp1(RWMol& mol, Atom* atom) { int neighborsO = 0; RWMol::ADJ_ITER nid, nid1, end1; boost::tie(nid1, end1) = mol.getAtomNeighbors(atom); + nid = end1; while (nid1 != end1) { Atom* otherAtom = mol.getAtomWithIdx(*nid1); if (otherAtom->getAtomicNum() == 8) { @@ -975,7 +976,7 @@ bool _Valence7SCleanUp1(RWMol& mol, Atom* atom) { } nid1++; } - if (neighborsC == 1 || neighborsO == 3) { + if (nid != end1 && (neighborsC == 1 || neighborsO == 3)) { mol.getBondBetweenAtoms(*nid, aid)->setBondType(Bond::SINGLE); Atom* otherAtom = mol.getAtomWithIdx(*nid); otherAtom->setFormalCharge(-1); @@ -1674,13 +1675,17 @@ RWMol* InchiToMol(const std::string& inchi, ExtraInchiReturnValues& rv, // clean up the molecule to be acceptable to RDKit if (m) { cleanUp(*m); - - if (sanitize) { - if (removeHs) { - MolOps::removeHs(*m, false, false); - } else { - MolOps::sanitizeMol(*m); + try { + if (sanitize) { + if (removeHs) { + MolOps::removeHs(*m, false, false); + } else { + MolOps::sanitizeMol(*m); + } } + } catch (const MolSanitizeException&) { + delete m; + throw; } // call assignStereochemistry just to be safe; otherwise, MolToSmiles may // overwrite E/Z and/or bond direction on double bonds. diff --git a/External/INCHI-API/test.cpp b/External/INCHI-API/test.cpp index ed318845a..115fa0bbd 100644 --- a/External/INCHI-API/test.cpp +++ b/External/INCHI-API/test.cpp @@ -777,6 +777,39 @@ void testGithub3365() { BOOST_LOG(rdInfoLog) << "done" << std::endl; } +void testGithub3645() { + BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl; + BOOST_LOG(rdInfoLog) << "testing github #3645: Seg fault when parsing InChI" + << std::endl; + + { + std::string inchi = + "InChI=1S/C9H22O3SSi/c1-5-9-13(14,10-6-2,11-7-3)12-8-4/h5-9H2,1-4H3"; + ExtraInchiReturnValues tmp; + bool ok = false; + try { + std::unique_ptr m{InchiToMol(inchi, tmp)}; + } catch (const MolSanitizeException &) { + ok = true; + } + TEST_ASSERT(ok); + } + { + std::string inchi = + "InChI=1S/C8H20O3SSi/c1-5-9-12(13,8-4,10-6-2)11-7-3/h5-8H2,1-4H3"; + ExtraInchiReturnValues tmp; + bool ok = false; + try { + std::unique_ptr m{InchiToMol(inchi, tmp)}; + } catch (const MolSanitizeException &) { + ok = true; + } + TEST_ASSERT(ok); + } + + BOOST_LOG(rdInfoLog) << "done" << std::endl; +} + //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* // //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* @@ -797,4 +830,5 @@ int main() { #endif testGithubIssue562(); testGithub3365(); + testGithub3645(); }