Unverified Commit 8526e7a4 authored by Axel Kohlmeyer's avatar Axel Kohlmeyer
Browse files

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

parent 4c328bf8
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -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
+14 −9
Original line number Diff line number Diff line
@@ -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
+5 −3
Original line number Diff line number Diff line
@@ -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;
+22 −14
Original line number Diff line number Diff line
@@ -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;
+5 −0
Original line number Diff line number Diff line
@@ -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.
Loading