checking return value of ?scanf() calls to detect problems parsing files

This commit is contained in:
Axel Kohlmeyer
2019-07-19 11:14:39 -04:00
parent 4c328bf846
commit 8526e7a4cd
7 changed files with 77 additions and 38 deletions

View File

@ -5890,6 +5890,11 @@ The element names in the ADP file do not match those requested. :dd
The element names in the EAM file do not match those requested. :dd
{Incorrect format of ... section in data file} :dt
Number or type of values per line in the given section of the data file
is not consistent with the requirements for this section. :dd
{Incorrect format in COMB potential file} :dt
Incorrect number of words per line in the potential file. :dd

View File

@ -397,28 +397,33 @@ void NEB::readfile(char *file, int flag)
open(file);
while (1) {
eof = fgets(line,MAXLINE,fp);
if (eof == NULL) error->one(FLERR,"Unexpected end of neb file");
if (eof == NULL) error->one(FLERR,"Unexpected end of NEB file");
start = &line[strspn(line," \t\n\v\f\r")];
if (*start != '\0' && *start != '#') break;
}
sscanf(line,"%d",&nlines);
int rv = sscanf(line,"%d",&nlines);
if (rv != 1) nlines = -1;
}
MPI_Bcast(&nlines,1,MPI_INT,0,uworld);
if (nlines < 0)
error->universe_all(FLERR,"Incorrectly formatted NEB file");
} else {
if (me == 0) {
if (ireplica) {
open(file);
while (1) {
eof = fgets(line,MAXLINE,fp);
if (eof == NULL) error->one(FLERR,"Unexpected end of neb file");
if (eof == NULL) error->one(FLERR,"Unexpected end of NEB file");
start = &line[strspn(line," \t\n\v\f\r")];
if (*start != '\0' && *start != '#') break;
}
sscanf(line,"%d",&nlines);
int rv = sscanf(line,"%d",&nlines);
if (rv != 1) nlines = -1;
} else nlines = 0;
}
MPI_Bcast(&nlines,1,MPI_INT,0,world);
if (nlines < 0)
error->all(FLERR,"Incorrectly formatted NEB file");
}
char *buffer = new char[CHUNK*MAXLINE];
@ -442,7 +447,7 @@ void NEB::readfile(char *file, int flag)
eofflag = comm->read_lines_from_file_universe(fp,nchunk,MAXLINE,buffer);
else
eofflag = comm->read_lines_from_file(fp,nchunk,MAXLINE,buffer);
if (eofflag) error->all(FLERR,"Unexpected end of neb file");
if (eofflag) error->all(FLERR,"Unexpected end of NEB file");
buf = buffer;
next = strchr(buf,'\n');
@ -451,7 +456,7 @@ void NEB::readfile(char *file, int flag)
*next = '\n';
if (nwords != ATTRIBUTE_PERLINE)
error->all(FLERR,"Incorrect atom format in neb file");
error->all(FLERR,"Incorrect atom format in NEB file");
// loop over lines of atom coords
// tokenize the line into values
@ -509,12 +514,12 @@ void NEB::readfile(char *file, int flag)
int ntotal;
MPI_Allreduce(&ncount,&ntotal,1,MPI_INT,MPI_SUM,uworld);
if (ntotal != nreplica*nlines)
error->universe_all(FLERR,"Invalid atom IDs in neb file");
error->universe_all(FLERR,"Invalid atom IDs in NEB file");
} else {
int ntotal;
MPI_Allreduce(&ncount,&ntotal,1,MPI_INT,MPI_SUM,world);
if (ntotal != nlines)
error->all(FLERR,"Invalid atom IDs in neb file");
error->all(FLERR,"Invalid atom IDs in NEB file");
}
// clean up

View File

@ -200,7 +200,7 @@ void FixQEqReax::pertype_parameters(char *arg)
return;
}
int i,itype,ntypes;
int i,itype,ntypes,rv;
double v1,v2,v3;
FILE *pf;
@ -216,9 +216,11 @@ void FixQEqReax::pertype_parameters(char *arg)
error->one(FLERR,"Fix qeq/reax parameter file could not be found");
for (i = 1; i <= ntypes && !feof(pf); i++) {
fscanf(pf,"%d %lg %lg %lg",&itype,&v1,&v2,&v3);
rv = fscanf(pf,"%d %lg %lg %lg",&itype,&v1,&v2,&v3);
if (rv != 4)
error->one(FLERR,"Fix qeq/reax: Incorrect format of param file");
if (itype < 1 || itype > ntypes)
error->one(FLERR,"Fix qeq/reax invalid atom type in param file");
error->one(FLERR,"Fix qeq/reax: invalid atom type in param file");
chi[itype] = v1;
eta[itype] = v2;
gamma[itype] = v3;

View File

@ -1071,7 +1071,7 @@ void Atom::data_vels(int n, char *buf, tagint id_offset)
void Atom::data_bonds(int n, char *buf, int *count, tagint id_offset,
int type_offset)
{
int m,tmp,itype;
int m,tmp,itype,rv;
tagint atom1,atom2;
char *next;
int newton_bond = force->newton_bond;
@ -1079,8 +1079,10 @@ void Atom::data_bonds(int n, char *buf, int *count, tagint id_offset,
for (int i = 0; i < n; i++) {
next = strchr(buf,'\n');
*next = '\0';
sscanf(buf,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT,
rv = sscanf(buf,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT,
&tmp,&itype,&atom1,&atom2);
if (rv != 4)
error->one(FLERR,"Incorrect format of Bonds section in data file");
if (id_offset) {
atom1 += id_offset;
atom2 += id_offset;
@ -1124,7 +1126,7 @@ void Atom::data_bonds(int n, char *buf, int *count, tagint id_offset,
void Atom::data_angles(int n, char *buf, int *count, tagint id_offset,
int type_offset)
{
int m,tmp,itype;
int m,tmp,itype,rv;
tagint atom1,atom2,atom3;
char *next;
int newton_bond = force->newton_bond;
@ -1132,8 +1134,10 @@ void Atom::data_angles(int n, char *buf, int *count, tagint id_offset,
for (int i = 0; i < n; i++) {
next = strchr(buf,'\n');
*next = '\0';
sscanf(buf,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT " " TAGINT_FORMAT,
rv = sscanf(buf,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT " " TAGINT_FORMAT,
&tmp,&itype,&atom1,&atom2,&atom3);
if (rv != 5)
error->one(FLERR,"Incorrect format of Angles section in data file");
if (id_offset) {
atom1 += id_offset;
atom2 += id_offset;
@ -1194,7 +1198,7 @@ void Atom::data_angles(int n, char *buf, int *count, tagint id_offset,
void Atom::data_dihedrals(int n, char *buf, int *count, tagint id_offset,
int type_offset)
{
int m,tmp,itype;
int m,tmp,itype,rv;
tagint atom1,atom2,atom3,atom4;
char *next;
int newton_bond = force->newton_bond;
@ -1202,9 +1206,11 @@ void Atom::data_dihedrals(int n, char *buf, int *count, tagint id_offset,
for (int i = 0; i < n; i++) {
next = strchr(buf,'\n');
*next = '\0';
sscanf(buf,"%d %d "
TAGINT_FORMAT " " TAGINT_FORMAT " " TAGINT_FORMAT " " TAGINT_FORMAT,
rv = sscanf(buf,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT
" " TAGINT_FORMAT " " TAGINT_FORMAT,
&tmp,&itype,&atom1,&atom2,&atom3,&atom4);
if (rv != 6)
error->one(FLERR,"Incorrect format of Dihedrals section in data file");
if (id_offset) {
atom1 += id_offset;
atom2 += id_offset;
@ -1283,7 +1289,7 @@ void Atom::data_dihedrals(int n, char *buf, int *count, tagint id_offset,
void Atom::data_impropers(int n, char *buf, int *count, tagint id_offset,
int type_offset)
{
int m,tmp,itype;
int m,tmp,itype,rv;
tagint atom1,atom2,atom3,atom4;
char *next;
int newton_bond = force->newton_bond;
@ -1291,9 +1297,11 @@ void Atom::data_impropers(int n, char *buf, int *count, tagint id_offset,
for (int i = 0; i < n; i++) {
next = strchr(buf,'\n');
*next = '\0';
sscanf(buf,"%d %d "
rv = sscanf(buf,"%d %d "
TAGINT_FORMAT " " TAGINT_FORMAT " " TAGINT_FORMAT " " TAGINT_FORMAT,
&tmp,&itype,&atom1,&atom2,&atom3,&atom4);
if (rv != 6)
error->one(FLERR,"Incorrect format of Impropers section in data file");
if (id_offset) {
atom1 += id_offset;
atom2 += id_offset;

View File

@ -426,6 +426,11 @@ E: Incorrect atom format in data file
Number of values per atom line in the data file is not consistent with
the atom style.
E: Incorrect format of ... section in data file
Number or type of values per line in the given section of the data file
is not consistent with the requirements for this section.
E: Invalid atom type in Atoms section of data file
Atom types must range from 1 to specified # of types.

View File

@ -59,8 +59,9 @@ int ReaderNative::read_time(bigint &ntimestep)
if (strstr(line,"ITEM: TIMESTEP") != line)
error->one(FLERR,"Dump file is incorrectly formatted");
read_lines(1);
sscanf(line,BIGINT_FORMAT,&ntimestep);
int rv = sscanf(line,BIGINT_FORMAT,&ntimestep);
if (rv != 1)
error->one(FLERR,"Dump file is incorrectly formatted");
return 0;
}
@ -73,7 +74,9 @@ void ReaderNative::skip()
{
read_lines(2);
bigint natoms;
sscanf(line,BIGINT_FORMAT,&natoms);
int rv = sscanf(line,BIGINT_FORMAT,&natoms);
if (rv != 1)
error->one(FLERR,"Dump file is incorrectly formatted");
read_lines(5);
@ -110,8 +113,12 @@ bigint ReaderNative::read_header(double box[3][3], int &boxinfo, int &triclinic,
int &xflag, int &yflag, int &zflag)
{
bigint natoms;
int rv;
read_lines(2);
sscanf(line,BIGINT_FORMAT,&natoms);
rv = sscanf(line,BIGINT_FORMAT,&natoms);
if (rv != 1)
error->one(FLERR,"Dump file is incorrectly formatted");
boxinfo = 1;
triclinic = 0;
@ -120,14 +127,19 @@ bigint ReaderNative::read_header(double box[3][3], int &boxinfo, int &triclinic,
if (line[strlen("ITEM: BOX BOUNDS ")] == 'x') triclinic = 1;
read_lines(1);
if (!triclinic) sscanf(line,"%lg %lg",&box[0][0],&box[0][1]);
else sscanf(line,"%lg %lg %lg",&box[0][0],&box[0][1],&box[0][2]);
if (!triclinic) rv = 2 - sscanf(line,"%lg %lg",&box[0][0],&box[0][1]);
else rv = 3 - sscanf(line,"%lg %lg %lg",&box[0][0],&box[0][1],&box[0][2]);
if (rv != 0) error->one(FLERR,"Dump file is incorrectly formatted");
read_lines(1);
if (!triclinic) sscanf(line,"%lg %lg",&box[1][0],&box[1][1]);
else sscanf(line,"%lg %lg %lg",&box[1][0],&box[1][1],&box[1][2]);
if (!triclinic) rv = 2 - sscanf(line,"%lg %lg",&box[1][0],&box[1][1]);
else rv = 3 - sscanf(line,"%lg %lg %lg",&box[1][0],&box[1][1],&box[1][2]);
if (rv != 0) error->one(FLERR,"Dump file is incorrectly formatted");
read_lines(1);
if (!triclinic) sscanf(line,"%lg %lg",&box[2][0],&box[2][1]);
else sscanf(line,"%lg %lg %lg",&box[2][0],&box[2][1],&box[2][2]);
if (!triclinic) rv = 2 - sscanf(line,"%lg %lg",&box[2][0],&box[2][1]);
else rv = 3 - sscanf(line,"%lg %lg %lg",&box[2][0],&box[2][1],&box[2][2]);
if (rv != 0) error->one(FLERR,"Dump file is incorrectly formatted");
read_lines(1);

View File

@ -170,7 +170,7 @@ bigint ReaderXYZ::read_header(double /*box*/[3][3], int &boxinfo, int &/*triclin
void ReaderXYZ::read_atoms(int n, int nfield, double **fields)
{
int i,m;
int i,m,rv;
char *eof;
int mytype;
double myx, myy, myz;
@ -180,7 +180,9 @@ void ReaderXYZ::read_atoms(int n, int nfield, double **fields)
if (eof == NULL) error->one(FLERR,"Unexpected end of dump file");
++nid;
sscanf(line,"%*s%lg%lg%lg", &myx, &myy, &myz);
rv = sscanf(line,"%*s%lg%lg%lg", &myx, &myy, &myz);
if (rv != 3)
error->one("Dump file is incorrectly formatted");
// XXX: we could insert an element2type translation here
// XXX: for now we flag unrecognized types as type 0,