Avoid stack overflow on Windows with CMake > 2.8.10.2
authorStefan Fleischmann <sfle@kth.se>
Fri, 8 Aug 2014 15:14:17 +0000 (17:14 +0200)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Mon, 11 Aug 2014 02:21:46 +0000 (04:21 +0200)
CMake used to add "/STACK:10000000" to the default linker flags. That
was removed in version 2.8.11-rc1. The default value used by MSVC is
apparently too small because mdrun crashes with a stack overflow when
built on Windows with MSVC or ICC and CMake newer than 2.8.10.2.

The issue is already fixed in GROMACS 5.0 and master by I1e9858ae3. This
is a backport for release-4-6 of that commit.

Change-Id: Ib9238e513da8e86049d9e56c82262055d3c8b349

src/gmxlib/gmxfio.c

index e9ede1322136f76bffa3c3437bdd6d67bee3be4a..69adcea129d0cfdb93c125471340c975696e0e65 100644 (file)
@@ -2,12 +2,11 @@
  * This file is part of the GROMACS molecular simulation package.
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2004, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
- * Copyright (c) 2012,2013, by the GROMACS development team, led by
- * David van der Spoel, Berk Hess, Erik Lindahl, and including many
- * others, as listed in the AUTHORS file in the top-level source
- * directory and at http://www.gromacs.org.
+ * Copyright (c) 2001-2004, The GROMACS development team.
+ * Copyright (c) 2013,2014, 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.
  *
  * GROMACS is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -683,11 +682,11 @@ static int gmx_fio_int_get_file_md5(t_fileio *fio, gmx_off_t offset,
 {
     /*1MB: large size important to catch almost identical files */
 #define CPT_CHK_LEN  1048576
-    md5_state_t   state;
-    unsigned char buf[CPT_CHK_LEN];
-    gmx_off_t     read_len;
-    gmx_off_t     seek_offset;
-    int           ret = -1;
+    md5_state_t    state;
+    unsigned char *buf;
+    gmx_off_t      read_len;
+    gmx_off_t      seek_offset;
+    int            ret = -1;
 
     seek_offset = offset - CPT_CHK_LEN;
     if (seek_offset < 0)
@@ -710,6 +709,7 @@ static int gmx_fio_int_get_file_md5(t_fileio *fio, gmx_off_t offset,
         return -1;
     }
 
+    snew(buf, CPT_CHK_LEN);
     /* the read puts the file position back to offset */
     if ((gmx_off_t)fread(buf, 1, read_len, fio->fp) != read_len)
     {
@@ -758,12 +758,10 @@ static int gmx_fio_int_get_file_md5(t_fileio *fio, gmx_off_t offset,
         md5_init(&state);
         md5_append(&state, buf, read_len);
         md5_finish(&state, digest);
-        return read_len;
-    }
-    else
-    {
-        return ret;
+        ret = read_len;
     }
+    sfree(buf);
+    return ret;
 }