Skip to content

Commit f760265

Browse files
committed
Some invalid smarts
1 parent 24c3b57 commit f760265

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

tool/smarts/src/main/java/org/openscience/cdk/smarts/Smarts.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,8 @@ else if (sub2 != null)
15731573
// final check
15741574
boolean finish() {
15751575
// check for unclosed rings, components, and branches
1576-
if (numRingOpens != 0 || curComponentId != 0 || !stack.isEmpty()) {
1576+
if (numRingOpens != 0 || curComponentId != 0 ||
1577+
!stack.isEmpty() || bond != null) {
15771578
error = "Unclosed ring, component group, or branch";
15781579
return false;
15791580
}
@@ -1805,6 +1806,8 @@ public boolean parse() {
18051806
case '!':
18061807
case '/':
18071808
case '\\':
1809+
if (prev == null)
1810+
return false;
18081811
unget();
18091812
if (!parseBondExpr())
18101813
return false;

tool/smarts/src/test/java/org/openscience/cdk/smarts/ParserTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Test;
2323
import org.openscience.cdk.CDKTestCase;
2424
import org.openscience.cdk.interfaces.IChemObjectBuilder;
25+
import org.openscience.cdk.isomorphism.matchers.Expr;
2526
import org.openscience.cdk.silent.SilentChemObjectBuilder;
2627
import org.openscience.cdk.smarts.Smarts;
2728

@@ -36,10 +37,16 @@
3637
*/
3738
public class ParserTest extends CDKTestCase {
3839

40+
private static final class InvalidSmarts extends Exception {
41+
public InvalidSmarts(String message) {
42+
super(message);
43+
}
44+
}
45+
3946
private void parse(String smarts, int flav) throws Exception {
4047
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
4148
if (!Smarts.parse(builder.newAtomContainer(), smarts, flav))
42-
throw new Exception(Smarts.getLastErrorMesg());
49+
throw new InvalidSmarts(Smarts.getLastErrorMesg());
4350
}
4451

4552
private void parse(String smarts) throws Exception {
@@ -1505,4 +1512,14 @@ public void atomMaps2() throws Exception {
15051512
parse("O=C1NCCSc2ccccc12");
15061513
}
15071514

1515+
@Test(expected = InvalidSmarts.class)
1516+
public void testBondPrefix() throws Exception {
1517+
parse("-CCO");
1518+
}
1519+
1520+
@Test(expected = InvalidSmarts.class)
1521+
public void trailingBond() throws Exception {
1522+
parse("CCO-");
1523+
}
1524+
15081525
}

0 commit comments

Comments
 (0)