From: Teemu Murtola Date: Wed, 20 May 2015 19:22:09 +0000 (+0300) Subject: Avoid crash with empty reference for 'same as' selection X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?a=commitdiff_plain;h=1cdc52e96ae0cffa45114472974122ff035e9b87;p=alexxy%2Fgromacs.git Avoid crash with empty reference for 'same as' selection Make 'same ... as none' selections not segfault. If there were no atoms in the reference group, the code that sorted the values and removed duplicates incorrectly set the number of values as one, resulting in incorrect memory access later. Fixes #1742 (segfault part). Change-Id: I9d14c30404121356ee3abf1a5575fb0baa82fb7b --- diff --git a/src/gromacs/selection/sm_same.cpp b/src/gromacs/selection/sm_same.cpp index 71192b842d..cef245c5d9 100644 --- a/src/gromacs/selection/sm_same.cpp +++ b/src/gromacs/selection/sm_same.cpp @@ -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. @@ -343,6 +343,10 @@ init_frame_same_int(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pb /* Collapse adjacent values, and check whether the array is sorted. */ d->bSorted = true; + if (d->nas == 0) + { + return; + } for (i = 1, j = 0; i < d->nas; ++i) { if (d->as.i[i] != d->as.i[j]) @@ -469,6 +473,10 @@ init_frame_same_str(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pb * For strings, it's unlikely that the values would be sorted originally, * so set bSorted always to false. */ d->bSorted = false; + if (d->nas == 0) + { + return; + } d->as_s_sorted[0] = d->as.s[0]; for (i = 1, j = 0; i < d->nas; ++i) { diff --git a/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesEmptyReferenceForSame.xml b/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesEmptyReferenceForSame.xml new file mode 100644 index 0000000000..49fb8476be --- /dev/null +++ b/src/gromacs/selection/tests/refdata/SelectionCollectionDataTest_HandlesEmptyReferenceForSame.xml @@ -0,0 +1,28 @@ + + + + + + same residue as none + same residue as none + false + + + same resname as none + same resname as none + false + + + + + + 0 + + + + + 0 + + + + diff --git a/src/gromacs/selection/tests/selectioncollection.cpp b/src/gromacs/selection/tests/selectioncollection.cpp index 66369c1a7d..6a40ad7462 100644 --- a/src/gromacs/selection/tests/selectioncollection.cpp +++ b/src/gromacs/selection/tests/selectioncollection.cpp @@ -1087,6 +1087,16 @@ TEST_F(SelectionCollectionDataTest, HandlesEmptySelectionWithUnevaluatedExpressi } +TEST_F(SelectionCollectionDataTest, HandlesEmptyReferenceForSame) +{ + static const char * const selections[] = { + "same residue as none", + "same resname as none" + }; + runTest("simple.gro", selections); +} + + TEST_F(SelectionCollectionDataTest, HandlesNumericComparisons) { static const char * const selections[] = {