Allocate memory for MD5 on heap
authorRoland Schulz <roland@utk.edu>
Wed, 29 Jan 2014 20:06:39 +0000 (15:06 -0500)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Thu, 30 Jan 2014 01:43:58 +0000 (02:43 +0100)
1MB on stack can cause issues with stack size limits (e.g. MSVC).

Change-Id: I1e9858ae32d78b4d494652f4aaed5354b9276669

src/gromacs/fileio/gmxfio.c

index 6bc342949ca6e9079d8ac0df33d0d9441433d5f5..0c8931847e85fbb06eed9ab06e4fc92cbe9a6b30 100644 (file)
@@ -2,8 +2,8 @@
  * 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,
- * Copyright (c) 2013, by the GROMACS development team, led by
+ * 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.
@@ -678,11 +678,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)
@@ -705,6 +705,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)
     {
@@ -753,12 +754,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;
 }