From ca04e8f31c62224dc8b01d19d229d21bf0cbe89a Mon Sep 17 00:00:00 2001
From: Axel Kohlmeyer <akohlmey@gmail.com>
Date: Thu, 6 Sep 2018 02:57:53 -0400
Subject: [PATCH] use snprintf() in a bunch of cases to avoid overflowing fixed
 size buffers with unchecked strings

---
 src/fix_ave_chunk.cpp     |  2 +-
 src/fix_ave_correlate.cpp |  2 +-
 src/fix_ave_histo.cpp     |  2 +-
 src/fix_ave_time.cpp      |  2 +-
 src/fix_enforce2d.cpp     |  2 +-
 src/fix_print.cpp         |  2 +-
 src/fix_property_atom.cpp |  8 ++++----
 src/fix_tmd.cpp           |  6 +++---
 src/force.cpp             |  4 ++--
 src/input.cpp             | 14 +++++++-------
 src/lammps.cpp            | 16 ++++++++--------
 src/memory.cpp            |  3 ++-
 src/modify.cpp            |  9 +++++----
 src/molecule.cpp          |  2 +-
 src/neighbor.cpp          |  2 +-
 src/pair_coul_streitz.cpp |  2 +-
 16 files changed, 40 insertions(+), 38 deletions(-)

diff --git a/src/fix_ave_chunk.cpp b/src/fix_ave_chunk.cpp
index 1aead70644..11ba4a41a5 100644
--- a/src/fix_ave_chunk.cpp
+++ b/src/fix_ave_chunk.cpp
@@ -225,7 +225,7 @@ FixAveChunk::FixAveChunk(LAMMPS *lmp, int narg, char **arg) :
         fp = fopen(arg[iarg+1],"w");
         if (fp == NULL) {
           char str[128];
-          sprintf(str,"Cannot open fix ave/chunk file %s",arg[iarg+1]);
+          snprintf(str,128,"Cannot open fix ave/chunk file %s",arg[iarg+1]);
           error->one(FLERR,str);
         }
       }
diff --git a/src/fix_ave_correlate.cpp b/src/fix_ave_correlate.cpp
index 198caf1514..5c95f20ec2 100644
--- a/src/fix_ave_correlate.cpp
+++ b/src/fix_ave_correlate.cpp
@@ -149,7 +149,7 @@ FixAveCorrelate::FixAveCorrelate(LAMMPS * lmp, int narg, char **arg):
         fp = fopen(arg[iarg+1],"w");
         if (fp == NULL) {
           char str[128];
-          sprintf(str,"Cannot open fix ave/correlate file %s",arg[iarg+1]);
+          snprintf(str,128,"Cannot open fix ave/correlate file %s",arg[iarg+1]);
           error->one(FLERR,str);
         }
       }
diff --git a/src/fix_ave_histo.cpp b/src/fix_ave_histo.cpp
index ce32167b74..a5bf8db557 100644
--- a/src/fix_ave_histo.cpp
+++ b/src/fix_ave_histo.cpp
@@ -937,7 +937,7 @@ void FixAveHisto::options(int iarg, int narg, char **arg)
         fp = fopen(arg[iarg+1],"w");
         if (fp == NULL) {
           char str[128];
-          sprintf(str,"Cannot open fix ave/histo file %s",arg[iarg+1]);
+          snprintf(str,128,"Cannot open fix ave/histo file %s",arg[iarg+1]);
           error->one(FLERR,str);
         }
       }
diff --git a/src/fix_ave_time.cpp b/src/fix_ave_time.cpp
index 53354ee066..50654b6561 100644
--- a/src/fix_ave_time.cpp
+++ b/src/fix_ave_time.cpp
@@ -1042,7 +1042,7 @@ void FixAveTime::options(int iarg, int narg, char **arg)
         fp = fopen(arg[iarg+1],"w");
         if (fp == NULL) {
           char str[128];
-          sprintf(str,"Cannot open fix ave/time file %s",arg[iarg+1]);
+          snprintf(str,128,"Cannot open fix ave/time file %s",arg[iarg+1]);
           error->one(FLERR,str);
         }
       }
diff --git a/src/fix_enforce2d.cpp b/src/fix_enforce2d.cpp
index ef7eb3a0ef..986ded5d16 100644
--- a/src/fix_enforce2d.cpp
+++ b/src/fix_enforce2d.cpp
@@ -78,7 +78,7 @@ void FixEnforce2D::init()
           flist[nfixlist++] = modify->fix[i];
         else {
           char msg[256];
-          sprintf(msg,"Fix enforce2d must be defined after fix %s",modify->fix[i]->style);
+          snprintf(msg,256,"Fix enforce2d must be defined after fix %s",modify->fix[i]->style);
           error->all(FLERR,msg);
         }
       }
diff --git a/src/fix_print.cpp b/src/fix_print.cpp
index 16218e0088..969fcf8140 100644
--- a/src/fix_print.cpp
+++ b/src/fix_print.cpp
@@ -60,7 +60,7 @@ FixPrint::FixPrint(LAMMPS *lmp, int narg, char **arg) :
         else fp = fopen(arg[iarg+1],"a");
         if (fp == NULL) {
           char str[128];
-          sprintf(str,"Cannot open fix print file %s",arg[iarg+1]);
+          snprintf(str,128,"Cannot open fix print file %s",arg[iarg+1]);
           error->one(FLERR,str);
         }
       }
diff --git a/src/fix_property_atom.cpp b/src/fix_property_atom.cpp
index 5b12b6def2..c89419f850 100644
--- a/src/fix_property_atom.cpp
+++ b/src/fix_property_atom.cpp
@@ -224,7 +224,7 @@ void FixPropertyAtom::read_data_section(char *keyword, int n, char *buf,
 
   if (nwords != nvalue+1) {
     char str[128];
-    sprintf(str,"Incorrect %s format in data file",keyword);
+    snprintf(str,128,"Incorrect %s format in data file",keyword);
     error->all(FLERR,str);
   }
 
@@ -242,7 +242,7 @@ void FixPropertyAtom::read_data_section(char *keyword, int n, char *buf,
     values[0] = strtok(buf," \t\n\r\f");
     if (values[0] == NULL) {
       char str[128];
-      sprintf(str,"Too few lines in %s section of data file",keyword);
+      snprintf(str,128,"Too few lines in %s section of data file",keyword);
       error->one(FLERR,str);
     }
     int format_ok = 1;
@@ -252,14 +252,14 @@ void FixPropertyAtom::read_data_section(char *keyword, int n, char *buf,
     }
     if (!format_ok) {
       char str[128];
-      sprintf(str,"Incorrect %s format in data file",keyword);
+      snprintf(str,128,"Incorrect %s format in data file",keyword);
       error->all(FLERR,str);
     }
 
     itag = ATOTAGINT(values[0]) + id_offset;
     if (itag <= 0 || itag > map_tag_max) {
       char str[128];
-      sprintf(str,"Invalid atom ID in %s section of data file",keyword);
+      snprintf(str,128,"Invalid atom ID in %s section of data file",keyword);
       error->one(FLERR,str);
     }
 
diff --git a/src/fix_tmd.cpp b/src/fix_tmd.cpp
index dd11dfd0c7..4cbb244bc0 100644
--- a/src/fix_tmd.cpp
+++ b/src/fix_tmd.cpp
@@ -75,7 +75,7 @@ nfileevery(0), fp(NULL), xf(NULL), xold(NULL)
       fp = fopen(arg[6],"w");
       if (fp == NULL) {
         char str[128];
-        sprintf(str,"Cannot open fix tmd file %s",arg[6]);
+        snprintf(str,128,"Cannot open fix tmd file %s",arg[6]);
         error->one(FLERR,str);
       }
       fprintf(fp,"%s %s\n","# Step rho_target rho_old gamma_back",
@@ -523,7 +523,7 @@ void FixTMD::open(char *file)
   else {
 #ifdef LAMMPS_GZIP
     char gunzip[128];
-    sprintf(gunzip,"gzip -c -d %s",file);
+    snprintf(gunzip,128,"gzip -c -d %s",file);
 
 #ifdef _WIN32
     fp = _popen(gunzip,"rb");
@@ -538,7 +538,7 @@ void FixTMD::open(char *file)
 
   if (fp == NULL) {
     char str[128];
-    sprintf(str,"Cannot open file %s",file);
+    snprintf(str,128,"Cannot open file %s",file);
     error->one(FLERR,str);
   }
 }
diff --git a/src/force.cpp b/src/force.cpp
index 8ee301982d..91fccd7197 100644
--- a/src/force.cpp
+++ b/src/force.cpp
@@ -182,8 +182,8 @@ void Force::init()
   if (pair_restart) {
     if (!pair) {
       char msg[128];
-      sprintf(msg,"Must re-specify non-restarted pair style (%s) "
-              "after read_restart", pair_restart);
+      snprintf(msg,128,"Must re-specify non-restarted pair style (%s) "
+               "after read_restart", pair_restart);
       error->all(FLERR,msg);
     }
   }
diff --git a/src/input.cpp b/src/input.cpp
index f41c8458ea..2834e36913 100644
--- a/src/input.cpp
+++ b/src/input.cpp
@@ -267,7 +267,7 @@ void Input::file(const char *filename)
     infile = fopen(filename,"r");
     if (infile == NULL) {
       char str[128];
-      sprintf(str,"Cannot open input script %s",filename);
+      snprintf(str,128,"Cannot open input script %s",filename);
       error->one(FLERR,str);
     }
     infiles[0] = infile;
@@ -526,7 +526,7 @@ void Input::substitute(char *&str, char *&str2, int &max, int &max2, int flag)
           *fmtflag='\0';
         }
 
-        sprintf(immediate,fmtstr,variable->compute_equal(var));
+        snprintf(immediate,256,fmtstr,variable->compute_equal(var));
         value = immediate;
 
       // single character variable name, e.g. $a
@@ -541,7 +541,7 @@ void Input::substitute(char *&str, char *&str2, int &max, int &max2, int flag)
 
       if (value == NULL) {
         char str[128];
-        sprintf(str,"Substitution for illegal variable %s",var);
+        snprintf(str,128,"Substitution for illegal variable %s",var);
         error->one(FLERR,str);
       }
       // check if storage in str2 needs to be expanded
@@ -1047,7 +1047,7 @@ void Input::include()
     infile = fopen(arg[0],"r");
     if (infile == NULL) {
       char str[128];
-      sprintf(str,"Cannot open input script %s",arg[0]);
+      snprintf(str,128,"Cannot open input script %s",arg[0]);
       error->one(FLERR,str);
     }
     infiles[nfile++] = infile;
@@ -1072,7 +1072,7 @@ void Input::jump()
       infile = fopen(arg[0],"r");
       if (infile == NULL) {
         char str[128];
-        sprintf(str,"Cannot open input script %s",arg[0]);
+        snprintf(str,128,"Cannot open input script %s",arg[0]);
         error->one(FLERR,str);
       }
       infiles[nfile-1] = infile;
@@ -1117,7 +1117,7 @@ void Input::log()
 
       if (logfile == NULL) {
         char str[128];
-        sprintf(str,"Cannot open logfile %s",arg[0]);
+        snprintf(str,128,"Cannot open logfile %s",arg[0]);
         error->one(FLERR,str);
       }
     }
@@ -1196,7 +1196,7 @@ void Input::print()
         else fp = fopen(arg[iarg+1],"a");
         if (fp == NULL) {
           char str[128];
-          sprintf(str,"Cannot open print file %s",arg[iarg+1]);
+          snprintf(str,128,"Cannot open print file %s",arg[iarg+1]);
           error->one(FLERR,str);
         }
       }
diff --git a/src/lammps.cpp b/src/lammps.cpp
index fcdfecf30a..04d8daa478 100644
--- a/src/lammps.cpp
+++ b/src/lammps.cpp
@@ -385,7 +385,7 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator)
       else infile = fopen(arg[inflag],"r");
       if (infile == NULL) {
         char str[128];
-        sprintf(str,"Cannot open input script %s",arg[inflag]);
+        snprintf(str,128,"Cannot open input script %s",arg[inflag]);
         error->one(FLERR,str);
       }
     }
@@ -416,7 +416,7 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator)
          screen = NULL;
        else {
          char str[128];
-         sprintf(str,"%s.%d",arg[screenflag],universe->iworld);
+         snprintf(str,128,"%s.%d",arg[screenflag],universe->iworld);
          screen = fopen(str,"w");
          if (screen == NULL) error->one(FLERR,"Cannot open screen file");
        }
@@ -424,7 +424,7 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator)
         screen = NULL;
       else {
         char str[128];
-        sprintf(str,"%s.%d",arg[partscreenflag],universe->iworld);
+        snprintf(str,128,"%s.%d",arg[partscreenflag],universe->iworld);
         screen = fopen(str,"w");
         if (screen == NULL) error->one(FLERR,"Cannot open screen file");
       } else screen = NULL;
@@ -440,7 +440,7 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator)
          logfile = NULL;
        else {
          char str[128];
-         sprintf(str,"%s.%d",arg[logflag],universe->iworld);
+         snprintf(str,128,"%s.%d",arg[logflag],universe->iworld);
          logfile = fopen(str,"w");
          if (logfile == NULL) error->one(FLERR,"Cannot open logfile");
        }
@@ -448,7 +448,7 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator)
         logfile = NULL;
       else {
         char str[128];
-        sprintf(str,"%s.%d",arg[partlogflag],universe->iworld);
+        snprintf(str,128,"%s.%d",arg[partlogflag],universe->iworld);
         logfile = fopen(str,"w");
         if (logfile == NULL) error->one(FLERR,"Cannot open logfile");
       } else logfile = NULL;
@@ -457,7 +457,7 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator)
       infile = fopen(arg[inflag],"r");
       if (infile == NULL) {
         char str[128];
-        sprintf(str,"Cannot open input script %s",arg[inflag]);
+        snprintf(str,128,"Cannot open input script %s",arg[inflag]);
         error->one(FLERR,str);
       }
     } else infile = NULL;
@@ -579,10 +579,10 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator)
 
   if (restartflag) {
     char cmd[128];
-    sprintf(cmd,"read_restart %s\n",rfile);
+    snprintf(cmd,128,"read_restart %s\n",rfile);
     if (restartremapflag) strcat(cmd," remap\n");
     input->one(cmd);
-    sprintf(cmd,"write_data %s",dfile);
+    snprintf(cmd,128,"write_data %s",dfile);
     for (iarg = wdfirst; iarg < wdlast; iarg++)
       sprintf(&cmd[strlen(cmd)]," %s",arg[iarg]);
     strcat(cmd," noinit\n");
diff --git a/src/memory.cpp b/src/memory.cpp
index 429cf75c00..7a23a23079 100644
--- a/src/memory.cpp
+++ b/src/memory.cpp
@@ -121,6 +121,7 @@ void Memory::sfree(void *ptr)
 void Memory::fail(const char *name)
 {
   char str[128];
-  sprintf(str,"Cannot create/grow a vector/array of pointers for %s",name);
+  snprintf(str,128,
+           "Cannot create/grow a vector/array of pointers for %s",name);
   error->one(FLERR,str);
 }
diff --git a/src/modify.cpp b/src/modify.cpp
index c5a680a3bd..8a0d1afa2c 100644
--- a/src/modify.cpp
+++ b/src/modify.cpp
@@ -239,7 +239,8 @@ void Modify::init()
   for (i = 0; i < nfix; i++)
     if (!fix[i]->dynamic_group_allow && group->dynamic[fix[i]->igroup]) {
       char str[128];
-      sprintf(str,"Fix %s does not allow use of dynamic group",fix[i]->id);
+      snprintf(str,128,
+               "Fix %s does not allow use of dynamic group",fix[i]->id);
       error->all(FLERR,str);
     }
 
@@ -247,7 +248,7 @@ void Modify::init()
     if (!compute[i]->dynamic_group_allow &&
         group->dynamic[compute[i]->igroup]) {
       char str[128];
-      sprintf(str,"Compute %s does not allow use of dynamic group",fix[i]->id);
+      snprintf(str,128,"Compute %s does not allow use of dynamic group",fix[i]->id);
       error->all(FLERR,str);
     }
 
@@ -889,7 +890,7 @@ void Modify::add_fix(int narg, char **arg, int trysuffix)
 
   if (fix[ifix] == NULL) {
     char str[128];
-    sprintf(str,"Unknown fix style %s",arg[2]);
+    snprintf(str,128,"Unknown fix style %s",arg[2]);
     error->all(FLERR,str);
   }
 
@@ -1191,7 +1192,7 @@ void Modify::add_compute(int narg, char **arg, int trysuffix)
 
   if (compute[ncompute] == NULL) {
     char str[128];
-    sprintf(str,"Unknown compute style %s",arg[2]);
+    snprintf(str,128,"Unknown compute style %s",arg[2]);
     error->all(FLERR,str);
   }
 
diff --git a/src/molecule.cpp b/src/molecule.cpp
index dfbe3e1e08..454459084a 100644
--- a/src/molecule.cpp
+++ b/src/molecule.cpp
@@ -1632,7 +1632,7 @@ void Molecule::open(char *file)
   fp = fopen(file,"r");
   if (fp == NULL) {
     char str[128];
-    sprintf(str,"Cannot open molecule file %s",file);
+    snprintf(str,128,"Cannot open molecule file %s",file);
     error->one(FLERR,str);
   }
 }
diff --git a/src/neighbor.cpp b/src/neighbor.cpp
index 759f84e29f..bc22fffc48 100644
--- a/src/neighbor.cpp
+++ b/src/neighbor.cpp
@@ -1408,7 +1408,7 @@ void Neighbor::init_topology()
 void Neighbor::print_pairwise_info()
 {
   int i,m;
-  char str[128];
+  char str[256];
   NeighRequest *rq;
   FILE *out;
 
diff --git a/src/pair_coul_streitz.cpp b/src/pair_coul_streitz.cpp
index 1d725f4ca8..920770ed7f 100644
--- a/src/pair_coul_streitz.cpp
+++ b/src/pair_coul_streitz.cpp
@@ -258,7 +258,7 @@ void PairCoulStreitz::read_file(char *file)
     fp = fopen(file,"r");
     if (fp == NULL) {
       char str[128];
-      sprintf(str,"Cannot open coul/streitz potential file %s",file);
+      snprintf(str,128,"Cannot open coul/streitz potential file %s",file);
       error->one(FLERR,str);
     }
   }
-- 
GitLab