Fix string handling for gcc 7
authorMark Abraham <mark.j.abraham@gmail.com>
Thu, 16 Mar 2017 14:29:48 +0000 (15:29 +0100)
committerErik Lindahl <erik.lindahl@gmail.com>
Sun, 19 Mar 2017 12:15:12 +0000 (13:15 +0100)
Pre-release gcc 7 warns about potential buffer overflows, so we may as
well fix them.

Change-Id: Ia154faf13b84473f073e5d553c7c275c19cbccec

src/gromacs/gmxana/gmx_xpm2ps.cpp
src/gromacs/gmxpreprocess/readir.cpp

index 49c46ba77521cd326a6cc8cecc524a6d35bd0eb0..be80038239315c36d666740d3598a118d8cd2edf 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,2016, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017, 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.
@@ -43,6 +43,7 @@
 #include <cstring>
 
 #include <algorithm>
+#include <string>
 
 #include "gromacs/commandline/pargs.h"
 #include "gromacs/commandline/viewit.h"
@@ -58,6 +59,7 @@
 #include "gromacs/utility/futil.h"
 #include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/stringutil.h"
 
 #define FUDGE 1.2
 #define DDD   2
@@ -786,7 +788,7 @@ void ps_mat(const char *outf, int nmat, t_matrix mat[], t_matrix mat2[],
             int mapoffset)
 {
     const char   *libm2p;
-    char          buf[256], *legend;
+    char         *legend;
     t_psdata      out;
     t_psrec       psrec, *psr;
     int           W, H;
@@ -923,19 +925,19 @@ void ps_mat(const char *outf, int nmat, t_matrix mat[], t_matrix mat2[],
             /* Print title, if any */
             ps_rgb(out, BLACK);
             ps_strfont(out, psr->titfont, psr->titfontsize);
+            std::string buf;
             if (!mat2 || (std::strcmp(mat[i].title, mat2[i].title) == 0))
             {
-                std::strcpy(buf, mat[i].title);
+                buf = mat[i].title;
             }
             else
             {
-                sprintf(buf, "%s / %s", mat[i].title, mat2[i].title);
+                buf = gmx::formatString("%s / %s", mat[i].title, mat2[i].title);
             }
             ps_ctext(out, x0+w/2, y0+box_height(&(mat[i]), psr)+psr->titfontsize,
-                     buf, eXCenter);
+                     buf.c_str(), eXCenter);
         }
-        sprintf(buf, "Here starts the filling of box #%d", i);
-        ps_comment(out, buf);
+        ps_comment(out, gmx::formatString("Here starts the filling of box #%d", i).c_str());
         for (x = 0; (x < mat[i].nx); x++)
         {
             int nexty;
index 40585ad45e19a66ad360f6c003f0bbd445d11095..6bbab36893f5d8ff45ad220be24febf8c4f1f65d 100644 (file)
@@ -45,6 +45,7 @@
 #include <cmath>
 
 #include <algorithm>
+#include <string>
 
 #include "gromacs/fileio/readinp.h"
 #include "gromacs/fileio/warninp.h"
@@ -68,6 +69,7 @@
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/stringutil.h"
 
 #define MAXPTR 254
 #define NOGID  255
@@ -2487,8 +2489,7 @@ void get_ir(const char *mdparin, const char *mdparout,
 
     if (strlen(is->deform) > 0 && ndeform != 6)
     {
-        sprintf(warn_buf, "Cannot parse exactly 6 box deformation velocities from string '%s'", is->deform);
-        warning_error(wi, warn_buf);
+        warning_error(wi, gmx::formatString("Cannot parse exactly 6 box deformation velocities from string '%s'", is->deform).c_str());
     }
     for (i = 0; i < 3; i++)
     {