Repository: rdkit/rdkit — pull request #8550 (cpp)
Title: Synthon space bad chiral count

#### Reference Issue
Fixes #8549


#### What does this implement/fix? Explain your changes.


#### Any other comments?



Resolved issues:
title: SynthonSpaceSearch bad chiral atom count
body: 
**Describe the bug**
When counting the atoms that are chiral or potentially chiral, it also counts stereo bonds.  That's clearly wrong, but if the bond idx is greater than the number of atoms it also throws an exception.

**To Reproduce**
```
  SynthonSpace space;
  std::istringstream iss(R"(SMILES	synton_id	synton#	reaction_id	release
C[U]	200011483129	1	4a	2024-09
c1c/c2n3/c1=C\C1=N/C(=C\c4c(C)c5c(n4[Mg]3)/C(=C3\N=C(\C=2)[C@@H](C)[C@@H]3C)[C@@H](C)C5=[U])C=C1	bad	2	4a	2024-09
)");
  bool cancelled = false;
  space.readStream(iss, cancelled);
```
This is a bit of FreedomSpace with a cut-down piece of an offending synthon.

**Expected behavior**
This molecule shouldn't throw an exception, although that's a side-effect of the bug itself.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Configuration (please complete the following information):**
 - RDKit version: current master branch.
 - OS: [e.g. Ubuntu 20.04]. macOS, but this is not OS dependent 
 - Python version (if relevant):
 - Are you using conda?
 - If you are using conda, which channel did you install the rdkit from?  
 - If you are not using conda: how did you install the RDKit? Built from source.

**Additional context**
Fix incoming.

Hints:

Fix patch:
diff --git a/Code/GraphMol/Descriptors/DCLV.cpp b/Code/GraphMol/Descriptors/DCLV.cpp
index 6a38e794d88..472a00a193a 100644
--- a/Code/GraphMol/Descriptors/DCLV.cpp
+++ b/Code/GraphMol/Descriptors/DCLV.cpp
@@ -288,7 +288,7 @@ struct State {
     elem->radius2 = rad * rad;
 
     if (dotDensity) {
-      long count = ((4.0 * M_PI) * rad * rad * dotDensity);
+      unsigned long count = ((4.0 * M_PI) * rad * rad * dotDensity);
       std::vector<DotStruct> dots(count);
 
       unsigned int equat = sqrt(M_PI * count);
diff --git a/Code/GraphMol/Kekulize.cpp b/Code/GraphMol/Kekulize.cpp
index 8805222cfc1..8a77330574c 100644
--- a/Code/GraphMol/Kekulize.cpp
+++ b/Code/GraphMol/Kekulize.cpp
@@ -140,7 +140,7 @@ void markDbondCands(RWMol &mol, const INT_VECT &allAtms,
 
     auto numAtomRings = mol.getRingInfo()->numAtomRings(at->getIdx());
     const auto &riVect = mol.getRingInfo()->atomMembers(at->getIdx());
-    auto numNonCandRings = std::count_if(
+    size_t numNonCandRings = std::count_if(
         riVect.begin(), riVect.end(),
         [&isRingNotCand](int ri) { return isRingNotCand.test(ri); });
     if (!at->getAtomicNum() && nonArNonDummyNbr < numAtomRings &&
diff --git a/Code/GraphMol/SynthonSpaceSearch/SynthonSpaceSearch_details.cpp b/Code/GraphMol/SynthonSpaceSearch/SynthonSpaceSearch_details.cpp
index 55b3b3b7d8e..0dedbaf64fe 100644
--- a/Code/GraphMol/SynthonSpaceSearch/SynthonSpaceSearch_details.cpp
+++ b/Code/GraphMol/SynthonSpaceSearch/SynthonSpaceSearch_details.cpp
@@ -873,6 +873,9 @@ unsigned int countChiralAtoms(ROMol &mol, unsigned int *numExcDummies) {
     *numExcDummies = 0;
   }
   for (auto &si : sis) {
+    if (si.type != Chirality::StereoType::Atom_Tetrahedral) {
+      continue;
+    }
     ++numChiralAtoms;
     if (numExcDummies) {
       auto atom = mol.getAtomWithIdx(si.centeredOn);


Test patch:
diff --git a/Code/GraphMol/SynthonSpaceSearch/substructure_search_catch_tests.cpp b/Code/GraphMol/SynthonSpaceSearch/substructure_search_catch_tests.cpp
index 0e4afb7e994..8a50027aaca 100644
--- a/Code/GraphMol/SynthonSpaceSearch/substructure_search_catch_tests.cpp
+++ b/Code/GraphMol/SynthonSpaceSearch/substructure_search_catch_tests.cpp
@@ -726,6 +726,7 @@ F[2*]	277310376-742385dd	2	fake-chiral
   auto res2 = space.substructureSearch(*mol2);
   CHECK(res2.getHitMolecules().size() == 2);
 }
+
 TEST_CASE("Chiral substructure search") {
   SynthonSpace space;
   std::istringstream iss(R"(SMILES	synton_id	synton#	reaction_id
@@ -759,4 +760,14 @@ F[2*]	277310376-742385dd	2	fake-chiral
   sparams.maxHitChiralAtoms = 0;
   auto res4 = space.substructureSearch(*qmol, mparams, sparams);
   CHECK(res4.getHitMolecules().size() == 0);
-}
\ No newline at end of file
+}
+
+TEST_CASE("Bad Chiral Atom Count") {
+  SynthonSpace space;
+  std::istringstream iss(R"(SMILES	synton_id	synton#	reaction_id	release
+C[U]	200011483129	1	4a	2024-09
+c1c/c2n3/c1=C\C1=N/C(=C\c4c(C)c5c(n4[Mg]3)/C(=C3\N=C(\C=2)[C@@H](C)[C@@H]3C)[C@@H](C)C5=[U])C=C1	bad	2	4a	2024-09
+)");
+  bool cancelled = false;
+  CHECK_NOTHROW(space.readStream(iss, cancelled));
+}