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

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