Fix crash with 'z of ...' within an expression
authorTeemu Murtola <teemu.murtola@gmail.com>
Fri, 2 Jan 2015 17:20:06 +0000 (19:20 +0200)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Sat, 3 Jan 2015 01:18:01 +0000 (02:18 +0100)
When a 'z of ...' construct was used within an arithmetic expression in
a selection, there were out-of-bounds array writes occurring during
selection compilation.  Initialize the number of values produced by such
constructs correctly to avoid this.

Change-Id: I7fb62c086bc02a96e90e35ab0a2d9944518900db

src/gromacs/selection/compiler.cpp
src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesKeywordOfPositionsInArithmetic.xml [new file with mode: 0644]
src/gromacs/selection/tests/selectioncollection.cpp

index d3a811c316cd93ea7fb77da55506a9570cc2ed6e..87ab39f543d0819c7a4c94459f91073effcd0847 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2009,2010,2011,2012,2013,2014,2015, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -1875,7 +1875,7 @@ init_method(const SelectionTreeElementPointer &sel, t_topology *top, int isize)
             if ((sel->flags & SEL_DYNAMIC)
                 && sel->v.type != GROUP_VALUE && sel->v.type != POS_VALUE)
             {
-                sel->v.nr = isize;
+                sel->v.nr = ((sel->flags & SEL_SINGLEVAL) ? 1 : isize);
             }
             /* If the method is char-valued, pre-allocate the strings. */
             if (sel->u.expr.method->flags & SMETH_CHARVAL)
diff --git a/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesKeywordOfPositionsInArithmetic.xml b/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesKeywordOfPositionsInArithmetic.xml
new file mode 100644 (file)
index 0000000..c012728
--- /dev/null
@@ -0,0 +1,48 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/xsl" href="referencedata.xsl"?>
+<ReferenceData>
+  <ParsedSelections Name="Parsed">
+    <ParsedSelection Name="Selection1">
+      <String Name="Input">x - y of cog of resnr 2 &lt; 0</String>
+      <String Name="Text">x - y of cog of resnr 2 &lt; 0</String>
+      <Bool Name="Dynamic">true</Bool>
+    </ParsedSelection>
+  </ParsedSelections>
+  <CompiledSelections Name="Compiled">
+    <Selection Name="Selection1">
+      <Sequence Name="Atoms">
+        <Int Name="Length">15</Int>
+        <Int>0</Int>
+        <Int>1</Int>
+        <Int>2</Int>
+        <Int>3</Int>
+        <Int>4</Int>
+        <Int>5</Int>
+        <Int>6</Int>
+        <Int>7</Int>
+        <Int>8</Int>
+        <Int>9</Int>
+        <Int>10</Int>
+        <Int>11</Int>
+        <Int>12</Int>
+        <Int>13</Int>
+        <Int>14</Int>
+      </Sequence>
+    </Selection>
+  </CompiledSelections>
+  <EvaluatedSelections Name="Frame1">
+    <Selection Name="Selection1">
+      <Sequence Name="Atoms">
+        <Int Name="Length">8</Int>
+        <Int>0</Int>
+        <Int>1</Int>
+        <Int>2</Int>
+        <Int>3</Int>
+        <Int>4</Int>
+        <Int>5</Int>
+        <Int>6</Int>
+        <Int>7</Int>
+      </Sequence>
+    </Selection>
+  </EvaluatedSelections>
+</ReferenceData>
index 0776b003e330bd7acde67e325c8d4cdb6a02d9bc..18fddac4c85c45ca98370208a74ef76c16584b41 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013,2014,2015, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -1146,6 +1146,15 @@ TEST_F(SelectionCollectionDataTest, HandlesKeywordOfPositions)
     runTest("simple.gro", selections);
 }
 
+TEST_F(SelectionCollectionDataTest, HandlesKeywordOfPositionsInArithmetic)
+{
+    static const char * const selections[] = {
+        "x - y of cog of resnr 2 < 0"
+    };
+    setFlags(TestFlags() | efTestEvaluation);
+    runTest("simple.gro", selections);
+}
+
 
 TEST_F(SelectionCollectionDataTest, HandlesNumericComparisons)
 {