use snprintf() in a bunch of cases to avoid overflowing fixed size buffers with unchecked strings

This commit is contained in:
Axel Kohlmeyer
2018-09-06 02:57:53 -04:00
parent 0b951840f2
commit ca04e8f31c
16 changed files with 40 additions and 38 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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");

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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);
}
}