Merge branch release-5-1 into release-2016
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / specbond.cpp
index 265e7d4f9330271684ebb16e12f6fd6a84e58f3c..74145c5cd244299318a357287c7716edebd75fed 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016, 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.
@@ -331,7 +331,8 @@ int mk_specbonds(t_atoms *pdba, rvec x[], gmx_bool bInteractive,
             for (j = i+1; (j < nspec); j++)
             {
                 aj = sgp[j];
-                if (is_bond(nsb, sb, pdba, ai, aj, d[i][j], &index_sb, &bSwap))
+                /* Ensure creation of at most nspec special bonds to avoid overflowing bonds[] */
+                if (nbonds < nspec && is_bond(nsb, sb, pdba, ai, aj, d[i][j], &index_sb, &bSwap))
                 {
                     fprintf(stderr, "%s %s-%d %s-%d and %s-%d %s-%d%s",
                             bInteractive ? "Link" : "Linking",