more buffer overflow avoiding through using snprintf()

This commit is contained in:
Axel Kohlmeyer
2018-09-06 09:57:43 -04:00
parent fbc1c1cfdd
commit 4d52cb9245
9 changed files with 26 additions and 26 deletions

View File

@ -362,7 +362,7 @@ void PairTable::read_table(Table *tb, char *file, char *keyword)
FILE *fp = force->open_potential(file); FILE *fp = force->open_potential(file);
if (fp == NULL) { if (fp == NULL) {
char str[128]; char str[128];
sprintf(str,"Cannot open file %s",file); snprintf(str,128,"Cannot open file %s",file);
error->one(FLERR,str); error->one(FLERR,str);
} }

View File

@ -706,7 +706,7 @@ void ReadData::command(int narg, char **arg)
} else { } else {
char str[128]; 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); error->all(FLERR,str);
} }
@ -1919,7 +1919,7 @@ void ReadData::open(char *file)
else { else {
#ifdef LAMMPS_GZIP #ifdef LAMMPS_GZIP
char gunzip[128]; char gunzip[128];
sprintf(gunzip,"gzip -c -d %s",file); snprintf(gunzip,128,"gzip -c -d %s",file);
#ifdef _WIN32 #ifdef _WIN32
fp = _popen(gunzip,"rb"); fp = _popen(gunzip,"rb");
@ -1934,7 +1934,7 @@ void ReadData::open(char *file)
if (fp == NULL) { if (fp == NULL) {
char str[128]; char str[128];
sprintf(str,"Cannot open file %s",file); snprintf(str,128,"Cannot open file %s",file);
error->one(FLERR,str); error->one(FLERR,str);
} }
} }

View File

@ -138,7 +138,7 @@ void ReadRestart::command(int narg, char **arg)
fp = fopen(hfile,"rb"); fp = fopen(hfile,"rb");
if (fp == NULL) { if (fp == NULL) {
char str[128]; char str[128];
sprintf(str,"Cannot open restart file %s",hfile); snprintf(str,128,"Cannot open restart file %s",hfile);
error->one(FLERR,str); error->one(FLERR,str);
} }
if (multiproc) delete [] hfile; if (multiproc) delete [] hfile;
@ -297,7 +297,7 @@ void ReadRestart::command(int narg, char **arg)
fp = fopen(procfile,"rb"); fp = fopen(procfile,"rb");
if (fp == NULL) { if (fp == NULL) {
char str[128]; char str[128];
sprintf(str,"Cannot open restart file %s",procfile); snprintf(str,128,"Cannot open restart file %s",procfile);
error->one(FLERR,str); error->one(FLERR,str);
} }
@ -369,7 +369,7 @@ void ReadRestart::command(int narg, char **arg)
fp = fopen(procfile,"rb"); fp = fopen(procfile,"rb");
if (fp == NULL) { if (fp == NULL) {
char str[128]; char str[128];
sprintf(str,"Cannot open restart file %s",procfile); snprintf(str,128,"Cannot open restart file %s",procfile);
error->one(FLERR,str); error->one(FLERR,str);
} }
delete [] procfile; delete [] procfile;

View File

@ -41,7 +41,7 @@ void Reader::open_file(const char *file)
else { else {
#ifdef LAMMPS_GZIP #ifdef LAMMPS_GZIP
char gunzip[1024]; char gunzip[1024];
sprintf(gunzip,"gzip -c -d %s",file); snprintf(gunzip,1024,"gzip -c -d %s",file);
#ifdef _WIN32 #ifdef _WIN32
fp = _popen(gunzip,"rb"); fp = _popen(gunzip,"rb");
@ -56,7 +56,7 @@ void Reader::open_file(const char *file)
if (fp == NULL) { if (fp == NULL) {
char str[128]; char str[128];
sprintf(str,"Cannot open file %s",file); snprintf(str,128,"Cannot open file %s",file);
error->one(FLERR,str); error->one(FLERR,str);
} }
} }

View File

@ -204,7 +204,7 @@ void Universe::add_world(char *str)
if (!valid) { if (!valid) {
char msg[128]; char msg[128];
sprintf(msg,"Invalid partition string '%s'",str); snprintf(msg,128,"Invalid partition string '%s'",str);
error->universe_all(FLERR,msg); error->universe_all(FLERR,msg);
} }
} else nper = nprocs; } else nper = nprocs;
@ -269,7 +269,7 @@ char *date2num(const char *version)
year = atoi(version); year = atoi(version);
} }
char *ver = new char[10]; char *ver = new char[64];
sprintf(ver,"%04d%02d%02d", year % 10000, month, day % 100); sprintf(ver,"%04d%02d%02d", year % 10000, month, day % 100);
return ver; return ver;

View File

@ -528,7 +528,7 @@ void Variable::set(int narg, char **arg)
for (int i = 0; i < n-1; i++) for (int i = 0; i < n-1; i++)
if (!isalnum(names[nvar][i]) && names[nvar][i] != '_') { if (!isalnum(names[nvar][i]) && names[nvar][i] != '_') {
char errmsg[128]; 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]); "characters or underscore",names[nvar]);
error->all(FLERR,errmsg); error->all(FLERR,errmsg);
} }
@ -586,7 +586,7 @@ int Variable::next(int narg, char **arg)
ivar = find(arg[iarg]); ivar = find(arg[iarg]);
if (ivar < 0) { if (ivar < 0) {
char errmsg[128]; 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); error->all(FLERR,errmsg);
} }
if (style[ivar] == ULOOP && style[find(arg[0])] == UNIVERSE) continue; 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); int ifunc = python->variable_match(data[ivar][0],name,0);
if (ifunc < 0) { if (ifunc < 0) {
char errmsg[128]; 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); error->all(FLERR,errmsg);
} }
python->invoke_function(ifunc,data[ivar][1]); 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); int ifix = modify->find_fix(word+2);
if (ifix < 0) { if (ifix < 0) {
char msg[128]; 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); print_var_error(FLERR,msg,ivar);
} }
Fix *fix = modify->fix[ifix]; Fix *fix = modify->fix[ifix];
@ -2032,8 +2032,8 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
argstack,nargstack,ivar)); argstack,nargstack,ivar));
else { else {
char msg[128]; char msg[128];
sprintf(msg,"Invalid math/group/special function '%s()'" snprintf(msg,128,"Invalid math/group/special function '%s()'"
"in variable formula", word); "in variable formula", word);
print_var_error(FLERR,msg,ivar); print_var_error(FLERR,msg,ivar);
} }
delete [] contents; delete [] contents;
@ -2092,7 +2092,7 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
int flag = output->thermo->evaluate_keyword(word,&value1); int flag = output->thermo->evaluate_keyword(word,&value1);
if (flag) { if (flag) {
char msg[128]; 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); print_var_error(FLERR,msg,ivar);
} }
if (tree) { if (tree) {
@ -4013,7 +4013,7 @@ int Variable::special_function(char *word, char *contents, Tree **tree,
int icompute = modify->find_compute(&args[0][2]); int icompute = modify->find_compute(&args[0][2]);
if (icompute < 0) { if (icompute < 0) {
char msg[128]; 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); print_var_error(FLERR,msg,ivar);
} }
compute = modify->compute[icompute]; compute = modify->compute[icompute];
@ -4656,7 +4656,7 @@ void Variable::print_var_error(const char *srcfile, int lineno,
if ((ivar >= 0) && (ivar < nvar)) { if ((ivar >= 0) && (ivar < nvar)) {
char msg[128]; 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); error->all(srcfile,lineno,msg);
} else error->all(srcfile,lineno,errmsg); } else error->all(srcfile,lineno,errmsg);
} }
@ -4944,7 +4944,7 @@ VarReader::VarReader(LAMMPS *lmp, char *name, char *file, int flag) :
fp = fopen(file,"r"); fp = fopen(file,"r");
if (fp == NULL) { if (fp == NULL) {
char str[128]; 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); error->one(FLERR,str);
} }
} }

View File

@ -51,7 +51,7 @@ void WriteCoeff::command(int narg, char **arg)
char str[256], coeff[256]; char str[256], coeff[256];
FILE *one = fopen(file,"wb+"); FILE *one = fopen(file,"wb+");
if (one == NULL) { if (one == NULL) {
sprintf(str,"Cannot open coeff file %s",file); snprintf(str,256,"Cannot open coeff file %s",file);
error->one(FLERR,str); error->one(FLERR,str);
} }
@ -86,7 +86,7 @@ void WriteCoeff::command(int narg, char **arg)
FILE *two = fopen(file+4,"w"); FILE *two = fopen(file+4,"w");
if (two == NULL) { 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); error->one(FLERR,str);
} }
fprintf(two,"# LAMMPS coeff file via write_coeff, version %s\n", fprintf(two,"# LAMMPS coeff file via write_coeff, version %s\n",

View File

@ -183,7 +183,7 @@ void WriteData::write(char *file)
fp = fopen(file,"w"); fp = fopen(file,"w");
if (fp == NULL) { if (fp == NULL) {
char str[128]; char str[128];
sprintf(str,"Cannot open data file %s",file); snprintf(str,128,"Cannot open data file %s",file);
error->one(FLERR,str); error->one(FLERR,str);
} }
} }

View File

@ -269,7 +269,7 @@ void WriteRestart::write(char *file)
fp = fopen(hfile,"wb"); fp = fopen(hfile,"wb");
if (fp == NULL) { if (fp == NULL) {
char str[128]; char str[128];
sprintf(str,"Cannot open restart file %s",hfile); snprintf(str,128,"Cannot open restart file %s",hfile);
error->one(FLERR,str); error->one(FLERR,str);
} }
if (multiproc) delete [] hfile; if (multiproc) delete [] hfile;
@ -334,7 +334,7 @@ void WriteRestart::write(char *file)
fp = fopen(multiname,"wb"); fp = fopen(multiname,"wb");
if (fp == NULL) { if (fp == NULL) {
char str[128]; char str[128];
sprintf(str,"Cannot open restart file %s",multiname); snprintf(str,128,"Cannot open restart file %s",multiname);
error->one(FLERR,str); error->one(FLERR,str);
} }
write_int(PROCSPERFILE,nclusterprocs); write_int(PROCSPERFILE,nclusterprocs);