From 0d4bf724c84ebe45924fc35e1473960539ca1149 Mon Sep 17 00:00:00 2001 From: Mark Abraham Date: Thu, 16 Mar 2017 15:29:48 +0100 Subject: [PATCH] Fix string handling for gcc 7 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 | 16 +++++++++------- src/gromacs/gmxpreprocess/readir.cpp | 5 +++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/gromacs/gmxana/gmx_xpm2ps.cpp b/src/gromacs/gmxana/gmx_xpm2ps.cpp index 49c46ba775..be80038239 100644 --- a/src/gromacs/gmxana/gmx_xpm2ps.cpp +++ b/src/gromacs/gmxana/gmx_xpm2ps.cpp @@ -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 #include +#include #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; diff --git a/src/gromacs/gmxpreprocess/readir.cpp b/src/gromacs/gmxpreprocess/readir.cpp index 40585ad45e..6bbab36893 100644 --- a/src/gromacs/gmxpreprocess/readir.cpp +++ b/src/gromacs/gmxpreprocess/readir.cpp @@ -45,6 +45,7 @@ #include #include +#include #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++) { -- 2.11.4.GIT