From 4d52cb9245b74d7d4690637738efffb1045ce1e5 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 6 Sep 2018 09:57:43 -0400 Subject: [PATCH] more buffer overflow avoiding through using snprintf() --- src/pair_table.cpp | 2 +- src/read_data.cpp | 6 +++--- src/read_restart.cpp | 6 +++--- src/reader.cpp | 4 ++-- src/universe.cpp | 4 ++-- src/variable.cpp | 20 ++++++++++---------- src/write_coeff.cpp | 4 ++-- src/write_data.cpp | 2 +- src/write_restart.cpp | 4 ++-- 9 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/pair_table.cpp b/src/pair_table.cpp index 5c43761cab..0d692caccb 100644 --- a/src/pair_table.cpp +++ b/src/pair_table.cpp @@ -362,7 +362,7 @@ void PairTable::read_table(Table *tb, char *file, char *keyword) FILE *fp = force->open_potential(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/read_data.cpp b/src/read_data.cpp index 5faa8f9646..26c406c1d6 100644 --- a/src/read_data.cpp +++ b/src/read_data.cpp @@ -706,7 +706,7 @@ void ReadData::command(int narg, char **arg) } else { char str[128]; - sprintf(str,"Unknown identifier in data file: %s",keyword); + snprintf(str,128,"Unknown identifier in data file: %s",keyword); error->all(FLERR,str); } @@ -1919,7 +1919,7 @@ void ReadData::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"); @@ -1934,7 +1934,7 @@ void ReadData::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/read_restart.cpp b/src/read_restart.cpp index 7d8e6ca395..9be035e1a1 100644 --- a/src/read_restart.cpp +++ b/src/read_restart.cpp @@ -138,7 +138,7 @@ void ReadRestart::command(int narg, char **arg) fp = fopen(hfile,"rb"); if (fp == NULL) { char str[128]; - sprintf(str,"Cannot open restart file %s",hfile); + snprintf(str,128,"Cannot open restart file %s",hfile); error->one(FLERR,str); } if (multiproc) delete [] hfile; @@ -297,7 +297,7 @@ void ReadRestart::command(int narg, char **arg) fp = fopen(procfile,"rb"); if (fp == NULL) { char str[128]; - sprintf(str,"Cannot open restart file %s",procfile); + snprintf(str,128,"Cannot open restart file %s",procfile); error->one(FLERR,str); } @@ -369,7 +369,7 @@ void ReadRestart::command(int narg, char **arg) fp = fopen(procfile,"rb"); if (fp == NULL) { char str[128]; - sprintf(str,"Cannot open restart file %s",procfile); + snprintf(str,128,"Cannot open restart file %s",procfile); error->one(FLERR,str); } delete [] procfile; diff --git a/src/reader.cpp b/src/reader.cpp index f6e8bd39eb..22a21812e6 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -41,7 +41,7 @@ void Reader::open_file(const char *file) else { #ifdef LAMMPS_GZIP char gunzip[1024]; - sprintf(gunzip,"gzip -c -d %s",file); + snprintf(gunzip,1024,"gzip -c -d %s",file); #ifdef _WIN32 fp = _popen(gunzip,"rb"); @@ -56,7 +56,7 @@ void Reader::open_file(const 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/universe.cpp b/src/universe.cpp index 45b4318ec9..410d12dcb3 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -204,7 +204,7 @@ void Universe::add_world(char *str) if (!valid) { char msg[128]; - sprintf(msg,"Invalid partition string '%s'",str); + snprintf(msg,128,"Invalid partition string '%s'",str); error->universe_all(FLERR,msg); } } else nper = nprocs; @@ -269,7 +269,7 @@ char *date2num(const char *version) year = atoi(version); } - char *ver = new char[10]; + char *ver = new char[64]; sprintf(ver,"%04d%02d%02d", year % 10000, month, day % 100); return ver; diff --git a/src/variable.cpp b/src/variable.cpp index 3c54eac1ef..1595456f33 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -528,7 +528,7 @@ void Variable::set(int narg, char **arg) for (int i = 0; i < n-1; i++) if (!isalnum(names[nvar][i]) && names[nvar][i] != '_') { char errmsg[128]; - sprintf(errmsg,"Variable name '%s' must have only alphanumeric " + snprintf(errmsg,128,"Variable name '%s' must have only alphanumeric " "characters or underscore",names[nvar]); error->all(FLERR,errmsg); } @@ -586,7 +586,7 @@ int Variable::next(int narg, char **arg) ivar = find(arg[iarg]); if (ivar < 0) { char errmsg[128]; - sprintf(errmsg,"Invalid variable '%s' in next command",arg[iarg]); + snprintf(errmsg,128,"Invalid variable '%s' in next command",arg[iarg]); error->all(FLERR,errmsg); } if (style[ivar] == ULOOP && style[find(arg[0])] == UNIVERSE) continue; @@ -886,7 +886,7 @@ char *Variable::retrieve(char *name) int ifunc = python->variable_match(data[ivar][0],name,0); if (ifunc < 0) { char errmsg[128]; - sprintf(errmsg,"Python variable '%s' does not match Python function",name); + snprintf(errmsg,128,"Python variable '%s' does not match Python function",name); error->all(FLERR,errmsg); } python->invoke_function(ifunc,data[ivar][1]); @@ -1597,7 +1597,7 @@ double Variable::evaluate(char *str, Tree **tree, int ivar) int ifix = modify->find_fix(word+2); if (ifix < 0) { char msg[128]; - sprintf(msg,"Invalid fix ID '%s' in variable formula",word+2); + snprintf(msg,128,"Invalid fix ID '%s' in variable formula",word+2); print_var_error(FLERR,msg,ivar); } Fix *fix = modify->fix[ifix]; @@ -2032,8 +2032,8 @@ double Variable::evaluate(char *str, Tree **tree, int ivar) argstack,nargstack,ivar)); else { char msg[128]; - sprintf(msg,"Invalid math/group/special function '%s()'" - "in variable formula", word); + snprintf(msg,128,"Invalid math/group/special function '%s()'" + "in variable formula", word); print_var_error(FLERR,msg,ivar); } delete [] contents; @@ -2092,7 +2092,7 @@ double Variable::evaluate(char *str, Tree **tree, int ivar) int flag = output->thermo->evaluate_keyword(word,&value1); if (flag) { char msg[128]; - sprintf(msg,"Invalid thermo keyword '%s' in variable formula",word); + snprintf(msg,128,"Invalid thermo keyword '%s' in variable formula",word); print_var_error(FLERR,msg,ivar); } if (tree) { @@ -4013,7 +4013,7 @@ int Variable::special_function(char *word, char *contents, Tree **tree, int icompute = modify->find_compute(&args[0][2]); if (icompute < 0) { char msg[128]; - sprintf(msg,"Invalid compute ID '%s' in variable formula",word+2); + snprintf(msg,128,"Invalid compute ID '%s' in variable formula",word+2); print_var_error(FLERR,msg,ivar); } compute = modify->compute[icompute]; @@ -4656,7 +4656,7 @@ void Variable::print_var_error(const char *srcfile, int lineno, if ((ivar >= 0) && (ivar < nvar)) { char msg[128]; - sprintf(msg,"Variable %s: %s",names[ivar],errmsg); + snprintf(msg,128,"Variable %s: %s",names[ivar],errmsg); error->all(srcfile,lineno,msg); } else error->all(srcfile,lineno,errmsg); } @@ -4944,7 +4944,7 @@ VarReader::VarReader(LAMMPS *lmp, char *name, char *file, int flag) : fp = fopen(file,"r"); if (fp == NULL) { char str[128]; - sprintf(str,"Cannot open file variable file %s",file); + snprintf(str,128,"Cannot open file variable file %s",file); error->one(FLERR,str); } } diff --git a/src/write_coeff.cpp b/src/write_coeff.cpp index 5f82a480fd..99172c09f0 100644 --- a/src/write_coeff.cpp +++ b/src/write_coeff.cpp @@ -51,7 +51,7 @@ void WriteCoeff::command(int narg, char **arg) char str[256], coeff[256]; FILE *one = fopen(file,"wb+"); if (one == NULL) { - sprintf(str,"Cannot open coeff file %s",file); + snprintf(str,256,"Cannot open coeff file %s",file); error->one(FLERR,str); } @@ -86,7 +86,7 @@ void WriteCoeff::command(int narg, char **arg) FILE *two = fopen(file+4,"w"); if (two == NULL) { - sprintf(str,"Cannot open coeff file %s",file+4); + snprintf(str,256,"Cannot open coeff file %s",file+4); error->one(FLERR,str); } fprintf(two,"# LAMMPS coeff file via write_coeff, version %s\n", diff --git a/src/write_data.cpp b/src/write_data.cpp index 96bf081157..f18e30de52 100644 --- a/src/write_data.cpp +++ b/src/write_data.cpp @@ -183,7 +183,7 @@ void WriteData::write(char *file) fp = fopen(file,"w"); if (fp == NULL) { char str[128]; - sprintf(str,"Cannot open data file %s",file); + snprintf(str,128,"Cannot open data file %s",file); error->one(FLERR,str); } } diff --git a/src/write_restart.cpp b/src/write_restart.cpp index 1bfbb382a8..edb6dfa7e8 100644 --- a/src/write_restart.cpp +++ b/src/write_restart.cpp @@ -269,7 +269,7 @@ void WriteRestart::write(char *file) fp = fopen(hfile,"wb"); if (fp == NULL) { char str[128]; - sprintf(str,"Cannot open restart file %s",hfile); + snprintf(str,128,"Cannot open restart file %s",hfile); error->one(FLERR,str); } if (multiproc) delete [] hfile; @@ -334,7 +334,7 @@ void WriteRestart::write(char *file) fp = fopen(multiname,"wb"); if (fp == NULL) { char str[128]; - sprintf(str,"Cannot open restart file %s",multiname); + snprintf(str,128,"Cannot open restart file %s",multiname); error->one(FLERR,str); } write_int(PROCSPERFILE,nclusterprocs);