more sscanf() return value checking

This commit is contained in:
Axel Kohlmeyer 2019-07-19 12:01:16 -04:00
parent d730ef5b19
commit e72aef2a96
No known key found for this signature in database
GPG Key ID: D9B44E93BF0C375A
3 changed files with 13 additions and 10 deletions

View File

@ -301,7 +301,8 @@ void ProcMap::custom_grid(char *cfile, int nprocs,
MPI_Bcast(&n,1,MPI_INT,0,world);
MPI_Bcast(line,n,MPI_CHAR,0,world);
sscanf(line,"%d %d %d",&procgrid[0],&procgrid[1],&procgrid[2]);
int rv = sscanf(line,"%d %d %d",&procgrid[0],&procgrid[1],&procgrid[2]);
if (rv != 3) error->all(FLERR,"Processors custom grid file is inconsistent");
int flag = 0;
if (procgrid[0]*procgrid[1]*procgrid[2] != nprocs) flag = 1;
@ -320,8 +321,10 @@ void ProcMap::custom_grid(char *cfile, int nprocs,
for (int i = 0; i < nprocs; i++) {
if (!fgets(line,MAXLINE,fp))
error->one(FLERR,"Unexpected end of custom file");
sscanf(line,"%d %d %d %d",
&cmap[i][0],&cmap[i][1],&cmap[i][2],&cmap[i][3]);
rv = sscanf(line,"%d %d %d %d",
&cmap[i][0],&cmap[i][1],&cmap[i][2],&cmap[i][3]);
if (rv != 4)
error->one(FLERR,"Processors custom grid file is inconsistent");
}
fclose(fp);
}

View File

@ -114,19 +114,19 @@ void Universe::reorder(char *style, char *arg)
// read nprocs lines
// uni2orig = inverse mapping
int me_orig,me_new;
sscanf(line,"%d %d",&me_orig,&me_new);
int me_orig,me_new,rv;
rv = sscanf(line,"%d %d",&me_orig,&me_new);
if (me_orig < 0 || me_orig >= nprocs ||
me_new < 0 || me_new >= nprocs)
me_new < 0 || me_new >= nprocs || rv != 2)
error->one(FLERR,"Invalid entry in -reorder file");
uni2orig[me_new] = me_orig;
for (int i = 1; i < nprocs; i++) {
if (!fgets(line,MAXLINE,fp))
error->one(FLERR,"Unexpected end of -reorder file");
sscanf(line,"%d %d",&me_orig,&me_new);
rv = sscanf(line,"%d %d",&me_orig,&me_new);
if (me_orig < 0 || me_orig >= nprocs ||
me_new < 0 || me_new >= nprocs)
me_new < 0 || me_new >= nprocs || rv != 2)
error->one(FLERR,"Invalid entry in -reorder file");
uni2orig[me_new] = me_orig;
}

View File

@ -5162,8 +5162,8 @@ int VarReader::read_peratom()
for (i = 0; i < nchunk; i++) {
next = strchr(buf,'\n');
*next = '\0';
sscanf(buf,TAGINT_FORMAT " %lg",&tag,&value);
if (tag <= 0 || tag > map_tag_max)
int rv = sscanf(buf,TAGINT_FORMAT " %lg",&tag,&value);
if (tag <= 0 || tag > map_tag_max || rv != 2)
error->one(FLERR,"Invalid atom ID in variable file");
if ((m = atom->map(tag)) >= 0) vstore[m] = value;
buf = next + 1;