From 2a7d2dee3652a229ad7a2a02e77ef558d63ed0a5 Mon Sep 17 00:00:00 2001
From: Axel Kohlmeyer <akohlmey@gmail.com>
Date: Thu, 10 Aug 2017 14:49:51 -0400
Subject: [PATCH] add more strict checking of data when parsing molecule files
 to detect format errors

---
 src/molecule.cpp | 433 +++++++++++++++++++++++------------------------
 1 file changed, 213 insertions(+), 220 deletions(-)

diff --git a/src/molecule.cpp b/src/molecule.cpp
index e0e9ec8aaf..b0fec4bcbc 100644
--- a/src/molecule.cpp
+++ b/src/molecule.cpp
@@ -427,47 +427,61 @@ void Molecule::read(int flag)
 
     // search line for header keywords and set corresponding variable
 
-    if (strstr(line,"atoms")) sscanf(line,"%d",&natoms);
-    else if (strstr(line,"bonds")) sscanf(line,"%d",&nbonds);
-    else if (strstr(line,"angles")) sscanf(line,"%d",&nangles);
-    else if (strstr(line,"dihedrals")) sscanf(line,"%d",&ndihedrals);
-    else if (strstr(line,"impropers")) sscanf(line,"%d",&nimpropers);
-
-    else if (strstr(line,"mass")) {
+    int nmatch = 0;
+    int nwant = 0;
+    if (strstr(line,"atoms")) {
+      nmatch = sscanf(line,"%d",&natoms);
+      nwant = 1;
+    } else if (strstr(line,"bonds")) {
+      nmatch = sscanf(line,"%d",&nbonds);
+      nwant = 1;
+    } else if (strstr(line,"angles")) {
+      nmatch = sscanf(line,"%d",&nangles);
+      nwant = 1;
+    } else if (strstr(line,"dihedrals")) {
+      nmatch = sscanf(line,"%d",&ndihedrals);
+      nwant = 1;
+    } else if (strstr(line,"impropers")) {
+      nmatch = sscanf(line,"%d",&nimpropers);
+      nwant = 1;
+    } else if (strstr(line,"mass")) {
       massflag = 1;
-      sscanf(line,"%lg",&masstotal);
+      nmatch = sscanf(line,"%lg",&masstotal);
+      nwant = 1;
       masstotal *= sizescale*sizescale*sizescale;
-    }
-    else if (strstr(line,"com")) {
+    } else if (strstr(line,"com")) {
       comflag = 1;
-      sscanf(line,"%lg %lg %lg",&com[0],&com[1],&com[2]);
+      nmatch = sscanf(line,"%lg %lg %lg",&com[0],&com[1],&com[2]);
+      nwant = 3;
       com[0] *= sizescale;
       com[1] *= sizescale;
       com[2] *= sizescale;
       if (domain->dimension == 2 && com[2] != 0.0)
         error->all(FLERR,"Molecule file z center-of-mass must be 0.0 for 2d");
-    }
-    else if (strstr(line,"inertia")) {
+    } else if (strstr(line,"inertia")) {
       inertiaflag = 1;
-      sscanf(line,"%lg %lg %lg %lg %lg %lg",
-             &itensor[0],&itensor[1],&itensor[2],
-             &itensor[3],&itensor[4],&itensor[5]);
-      itensor[0] *= sizescale*sizescale*sizescale*sizescale*sizescale;
-      itensor[1] *= sizescale*sizescale*sizescale*sizescale*sizescale;
-      itensor[2] *= sizescale*sizescale*sizescale*sizescale*sizescale;
-      itensor[3] *= sizescale*sizescale*sizescale*sizescale*sizescale;
-      itensor[4] *= sizescale*sizescale*sizescale*sizescale*sizescale;
-      itensor[5] *= sizescale*sizescale*sizescale*sizescale*sizescale;
-    }
-    else if (strstr(line,"body")) {
+      nmatch = sscanf(line,"%lg %lg %lg %lg %lg %lg",
+                      &itensor[0],&itensor[1],&itensor[2],
+                      &itensor[3],&itensor[4],&itensor[5]);
+      nwant = 6;
+      const double scale5 = sizescale*sizescale*sizescale*sizescale*sizescale;
+      itensor[0] *= scale5;
+      itensor[1] *= scale5;
+      itensor[2] *= scale5;
+      itensor[3] *= scale5;
+      itensor[4] *= scale5;
+      itensor[5] *= scale5;
+    } else if (strstr(line,"body")) {
       bodyflag = 1;
       avec_body = (AtomVecBody *) atom->style_match("body");
       if (!avec_body) 
         error->all(FLERR,"Molecule file requires atom style body");
-      sscanf(line,"%d %d",&nibody,&ndbody);
-    }
+      nmatch = sscanf(line,"%d %d",&nibody,&ndbody);
+      nwant = 2;
+    } else break;
 
-    else break;
+    if (nmatch != nwant)
+      error->all(FLERR,"Invalid header in molecule file");
   }
 
   // error checks
@@ -493,7 +507,7 @@ void Molecule::read(int flag)
 
   // loop over sections of molecule file
 
-  while (strlen(keyword)) {
+  while (strlen(keyword) > 0) {
     if (strcmp(keyword,"Coords") == 0) {
       xflag = 1;
       if (flag) coords(line);
@@ -517,22 +531,22 @@ void Molecule::read(int flag)
 
     } else if (strcmp(keyword,"Bonds") == 0) {
       if (nbonds == 0)
-	error->all(FLERR,"Molecule file has bonds but no nbonds setting");
+        error->all(FLERR,"Molecule file has bonds but no nbonds setting");
       bondflag = tag_require = 1;
       bonds(flag,line);
     } else if (strcmp(keyword,"Angles") == 0) {
       if (nangles == 0)
-	error->all(FLERR,"Molecule file has angles but no nangles setting");
+        error->all(FLERR,"Molecule file has angles but no nangles setting");
       angleflag = tag_require = 1;
       angles(flag,line);
     } else if (strcmp(keyword,"Dihedrals") == 0) {
       if (ndihedrals == 0) error->all(FLERR,"Molecule file has dihedrals "
-				      "but no ndihedrals setting");
+                                      "but no ndihedrals setting");
       dihedralflag = tag_require = 1;
       dihedrals(flag,line);
     } else if (strcmp(keyword,"Impropers") == 0) {
       if (nimpropers == 0) error->all(FLERR,"Molecule file has impropers "
-				      "but no nimpropers setting");
+                                      "but no nimpropers setting");
       improperflag = tag_require = 1;
       impropers(flag,line);
 
@@ -552,26 +566,26 @@ void Molecule::read(int flag)
       shakeatomflag = tag_require = 1;
       if (shaketypeflag) shakeflag = 1;
       if (!shakeflagflag)
-	error->all(FLERR,"Molecule file shake flags not before shake atoms");
+        error->all(FLERR,"Molecule file shake flags not before shake atoms");
       if (flag) shakeatom_read(line);
       else skip_lines(natoms,line);
     } else if (strcmp(keyword,"Shake Bond Types") == 0) {
       shaketypeflag = 1;
       if (shakeatomflag) shakeflag = 1;
       if (!shakeflagflag)
-	error->all(FLERR,"Molecule file shake flags not before shake bonds");
+        error->all(FLERR,"Molecule file shake flags not before shake bonds");
       if (flag) shaketype_read(line);
       else skip_lines(natoms,line);
 
     } else if (strcmp(keyword,"Body Integers") == 0) {
       if (bodyflag == 0 || nibody == 0)
-	error->all(FLERR,"Molecule file has body params "
+        error->all(FLERR,"Molecule file has body params "
                    "but no setting for them");
       ibodyflag = 1;
       body(flag,0,line);
     } else if (strcmp(keyword,"Body Doubles") == 0) {
       if (bodyflag == 0 || ndbody == 0)
-	error->all(FLERR,"Molecule file has body params "
+        error->all(FLERR,"Molecule file has body params "
                    "but no setting for them");
       dbodyflag = 1;
       body(flag,1,line);
@@ -618,7 +632,7 @@ void Molecule::read(int flag)
 
   // body particle must have natom = 1
   // set radius by having body class compute its own radius
-  
+
   if (bodyflag) {
     radiusflag = 1;
     if (natoms != 1) 
@@ -641,12 +655,9 @@ void Molecule::coords(char *line)
   int tmp;
   for (int i = 0; i < natoms; i++) {
     readline(line);
-    if (i == 0) {
-      int nwords = atom->count_words(line);
-      if (nwords != 4)
-        error->all(FLERR,"Invalid Coords section in molecule file");
-    }
-    sscanf(line,"%d %lg %lg %lg",&tmp,&x[i][0],&x[i][1],&x[i][2]);
+    if (4 != sscanf(line,"%d %lg %lg %lg",&tmp,&x[i][0],&x[i][1],&x[i][2]))
+      error->all(FLERR,"Invalid Coords section in molecule file");
+
     x[i][0] *= sizescale;
     x[i][1] *= sizescale;
     x[i][2] *= sizescale;
@@ -669,12 +680,8 @@ void Molecule::types(char *line)
   int tmp;
   for (int i = 0; i < natoms; i++) {
     readline(line);
-    if (i == 0) {
-      int nwords = atom->count_words(line);
-      if (nwords != 2)
-        error->all(FLERR,"Invalid Types section in molecule file");
-    }
-    sscanf(line,"%d %d",&tmp,&type[i]);
+    if (2 != sscanf(line,"%d %d",&tmp,&type[i]))
+      error->all(FLERR,"Invalid Types section in molecule file");
     type[i] += toffset;
   }
 
@@ -695,12 +702,8 @@ void Molecule::charges(char *line)
   int tmp;
   for (int i = 0; i < natoms; i++) {
     readline(line);
-    if (i == 0) {
-      int nwords = atom->count_words(line);
-      if (nwords != 2)
-        error->all(FLERR,"Invalid Charges section in molecule file");
-    }
-    sscanf(line,"%d %lg",&tmp,&q[i]);
+    if (2 != sscanf(line,"%d %lg",&tmp,&q[i]))
+      error->all(FLERR,"Invalid Charges section in molecule file");
   }
 }
 
@@ -714,12 +717,8 @@ void Molecule::diameters(char *line)
   maxradius = 0.0;
   for (int i = 0; i < natoms; i++) {
     readline(line);
-    if (i == 0) {
-      int nwords = atom->count_words(line);
-      if (nwords != 2)
-        error->all(FLERR,"Invalid Diameters section in molecule file");
-    }
-    sscanf(line,"%d %lg",&tmp,&radius[i]);
+    if (2 != sscanf(line,"%d %lg",&tmp,&radius[i]))
+      error->all(FLERR,"Invalid Diameters section in molecule file");
     radius[i] *= sizescale;
     radius[i] *= 0.5;
     maxradius = MAX(maxradius,radius[i]);
@@ -739,12 +738,8 @@ void Molecule::masses(char *line)
   int tmp;
   for (int i = 0; i < natoms; i++) {
     readline(line);
-    if (i == 0) {
-      int nwords = atom->count_words(line);
-      if (nwords != 2)
-        error->all(FLERR,"Invalid Masses section in molecule file");
-    }
-    sscanf(line,"%d %lg",&tmp,&rmass[i]);
+    if (2 != sscanf(line,"%d %lg",&tmp,&rmass[i]))
+      error->all(FLERR,"Invalid Masses section in molecule file");
     rmass[i] *= sizescale*sizescale*sizescale;
   }
 
@@ -773,17 +768,13 @@ void Molecule::bonds(int flag, char *line)
 
   for (int i = 0; i < nbonds; i++) {
     readline(line);
-    if (i == 0) {
-      int nwords = atom->count_words(line);
-      if (nwords != 4)
-        error->all(FLERR,"Invalid Bonds section in molecule file");
-    }
-    sscanf(line,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT,
-           &tmp,&itype,&atom1,&atom2);
+    if (4 != sscanf(line,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT,
+           &tmp,&itype,&atom1,&atom2))
+      error->all(FLERR,"Invalid Bonds section in molecule file");
     itype += boffset;
 
     if (atom1 <= 0 || atom1 > natoms ||
-	atom2 <= 0 || atom2 > natoms)
+        atom2 <= 0 || atom2 > natoms)
       error->one(FLERR,"Invalid atom ID in Bonds section of molecule file");
     if (itype <= 0)
       error->one(FLERR,"Invalid bond type in Bonds section of molecule file");
@@ -795,10 +786,10 @@ void Molecule::bonds(int flag, char *line)
       bond_atom[m][num_bond[m]] = atom2;
       num_bond[m]++;
       if (newton_bond == 0) {
-	m = atom2-1;
-	bond_type[m][num_bond[m]] = itype;
-	bond_atom[m][num_bond[m]] = atom1;
-	num_bond[m]++;
+        m = atom2-1;
+        bond_type[m][num_bond[m]] = itype;
+        bond_atom[m][num_bond[m]] = atom1;
+        num_bond[m]++;
       }
     } else {
       count[atom1-1]++;
@@ -835,13 +826,9 @@ void Molecule::angles(int flag, char *line)
 
   for (int i = 0; i < nangles; i++) {
     readline(line);
-    if (i == 0) {
-      int nwords = atom->count_words(line);
-      if (nwords != 5)
-        error->all(FLERR,"Invalid Angles section in molecule file");
-    }
-    sscanf(line,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT " " TAGINT_FORMAT,
-           &tmp,&itype,&atom1,&atom2,&atom3);
+    if (5 != sscanf(line,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT " " TAGINT_FORMAT,
+           &tmp,&itype,&atom1,&atom2,&atom3))
+      error->all(FLERR,"Invalid Angles section in molecule file");
     itype += aoffset;
 
     if (atom1 <= 0 || atom1 > natoms ||
@@ -860,24 +847,24 @@ void Molecule::angles(int flag, char *line)
       angle_atom3[m][num_angle[m]] = atom3;
       num_angle[m]++;
       if (newton_bond == 0) {
-	m = atom1-1;
-	angle_type[m][num_angle[m]] = itype;
-	angle_atom1[m][num_angle[m]] = atom1;
-	angle_atom2[m][num_angle[m]] = atom2;
-	angle_atom3[m][num_angle[m]] = atom3;
-	num_angle[m]++;
-	m = atom3-1;
-	angle_type[m][num_angle[m]] = itype;
-	angle_atom1[m][num_angle[m]] = atom1;
-	angle_atom2[m][num_angle[m]] = atom2;
-	angle_atom3[m][num_angle[m]] = atom3;
-	num_angle[m]++;
+        m = atom1-1;
+        angle_type[m][num_angle[m]] = itype;
+        angle_atom1[m][num_angle[m]] = atom1;
+        angle_atom2[m][num_angle[m]] = atom2;
+        angle_atom3[m][num_angle[m]] = atom3;
+        num_angle[m]++;
+        m = atom3-1;
+        angle_type[m][num_angle[m]] = itype;
+        angle_atom1[m][num_angle[m]] = atom1;
+        angle_atom2[m][num_angle[m]] = atom2;
+        angle_atom3[m][num_angle[m]] = atom3;
+        num_angle[m]++;
       }
     } else {
       count[atom2-1]++;
       if (newton_bond == 0) {
-	count[atom1-1]++;
-	count[atom3-1]++;
+        count[atom1-1]++;
+        count[atom3-1]++;
       }
     }
   }
@@ -911,14 +898,10 @@ void Molecule::dihedrals(int flag, char *line)
 
   for (int i = 0; i < ndihedrals; i++) {
     readline(line);
-    if (i == 0) {
-      int nwords = atom->count_words(line);
-      if (nwords != 6)
-        error->all(FLERR,"Invalid Dihedrals section in molecule file");
-    }
-    sscanf(line,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT " "
+    if (6 != sscanf(line,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT " "
            TAGINT_FORMAT " " TAGINT_FORMAT " ",
-           &tmp,&itype,&atom1,&atom2,&atom3,&atom4);
+           &tmp,&itype,&atom1,&atom2,&atom3,&atom4))
+      error->all(FLERR,"Invalid Dihedrals section in molecule file");
     itype += doffset;
 
     if (atom1 <= 0 || atom1 > natoms ||
@@ -926,10 +909,10 @@ void Molecule::dihedrals(int flag, char *line)
         atom3 <= 0 || atom3 > natoms ||
         atom4 <= 0 || atom4 > natoms)
       error->one(FLERR,
-		 "Invalid atom ID in dihedrals section of molecule file");
+                 "Invalid atom ID in dihedrals section of molecule file");
     if (itype <= 0)
       error->one(FLERR,
-		 "Invalid dihedral type in dihedrals section of molecule file");
+                 "Invalid dihedral type in dihedrals section of molecule file");
 
     if (flag) {
       m = atom2-1;
@@ -941,34 +924,34 @@ void Molecule::dihedrals(int flag, char *line)
       dihedral_atom4[m][num_dihedral[m]] = atom4;
       num_dihedral[m]++;
       if (newton_bond == 0) {
-	m = atom1-1;
-	dihedral_type[m][num_dihedral[m]] = itype;
-	dihedral_atom1[m][num_dihedral[m]] = atom1;
-	dihedral_atom2[m][num_dihedral[m]] = atom2;
-	dihedral_atom3[m][num_dihedral[m]] = atom3;
-	dihedral_atom4[m][num_dihedral[m]] = atom4;
-	num_dihedral[m]++;
-	m = atom3-1;
-	dihedral_type[m][num_dihedral[m]] = itype;
-	dihedral_atom1[m][num_dihedral[m]] = atom1;
-	dihedral_atom2[m][num_dihedral[m]] = atom2;
-	dihedral_atom3[m][num_dihedral[m]] = atom3;
-	dihedral_atom4[m][num_dihedral[m]] = atom4;
-	num_dihedral[m]++;
-	m = atom4-1;
-	dihedral_type[m][num_dihedral[m]] = itype;
-	dihedral_atom1[m][num_dihedral[m]] = atom1;
-	dihedral_atom2[m][num_dihedral[m]] = atom2;
-	dihedral_atom3[m][num_dihedral[m]] = atom3;
-	dihedral_atom4[m][num_dihedral[m]] = atom4;
-	num_dihedral[m]++;
+        m = atom1-1;
+        dihedral_type[m][num_dihedral[m]] = itype;
+        dihedral_atom1[m][num_dihedral[m]] = atom1;
+        dihedral_atom2[m][num_dihedral[m]] = atom2;
+        dihedral_atom3[m][num_dihedral[m]] = atom3;
+        dihedral_atom4[m][num_dihedral[m]] = atom4;
+        num_dihedral[m]++;
+        m = atom3-1;
+        dihedral_type[m][num_dihedral[m]] = itype;
+        dihedral_atom1[m][num_dihedral[m]] = atom1;
+        dihedral_atom2[m][num_dihedral[m]] = atom2;
+        dihedral_atom3[m][num_dihedral[m]] = atom3;
+        dihedral_atom4[m][num_dihedral[m]] = atom4;
+        num_dihedral[m]++;
+        m = atom4-1;
+        dihedral_type[m][num_dihedral[m]] = itype;
+        dihedral_atom1[m][num_dihedral[m]] = atom1;
+        dihedral_atom2[m][num_dihedral[m]] = atom2;
+        dihedral_atom3[m][num_dihedral[m]] = atom3;
+        dihedral_atom4[m][num_dihedral[m]] = atom4;
+        num_dihedral[m]++;
       }
     } else {
       count[atom2-1]++;
       if (newton_bond == 0) {
-	count[atom1-1]++;
-	count[atom3-1]++;
-	count[atom4-1]++;
+        count[atom1-1]++;
+        count[atom3-1]++;
+        count[atom4-1]++;
       }
     }
   }
@@ -1002,14 +985,10 @@ void Molecule::impropers(int flag, char *line)
 
   for (int i = 0; i < nimpropers; i++) {
     readline(line);
-    if (i == 0) {
-      int nwords = atom->count_words(line);
-      if (nwords != 6)
-        error->all(FLERR,"Invalid Impropers section in molecule file");
-    }
-    sscanf(line,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT " "
+    if (6 != sscanf(line,"%d %d " TAGINT_FORMAT " " TAGINT_FORMAT " "
            TAGINT_FORMAT " " TAGINT_FORMAT " ",
-           &tmp,&itype,&atom1,&atom2,&atom3,&atom4);
+           &tmp,&itype,&atom1,&atom2,&atom3,&atom4))
+      error->all(FLERR,"Invalid Impropers section in molecule file");
     itype += ioffset;
 
     if (atom1 <= 0 || atom1 > natoms ||
@@ -1017,10 +996,10 @@ void Molecule::impropers(int flag, char *line)
         atom3 <= 0 || atom3 > natoms ||
         atom4 <= 0 || atom4 > natoms)
       error->one(FLERR,
-		 "Invalid atom ID in impropers section of molecule file");
+                 "Invalid atom ID in impropers section of molecule file");
     if (itype <= 0)
       error->one(FLERR,
-		 "Invalid improper type in impropers section of molecule file");
+                 "Invalid improper type in impropers section of molecule file");
 
     if (flag) {
       m = atom2-1;
@@ -1032,34 +1011,34 @@ void Molecule::impropers(int flag, char *line)
       improper_atom4[m][num_improper[m]] = atom4;
       num_improper[m]++;
       if (newton_bond == 0) {
-	m = atom1-1;
-	improper_type[m][num_improper[m]] = itype;
-	improper_atom1[m][num_improper[m]] = atom1;
-	improper_atom2[m][num_improper[m]] = atom2;
-	improper_atom3[m][num_improper[m]] = atom3;
-	improper_atom4[m][num_improper[m]] = atom4;
-	num_improper[m]++;
-	m = atom3-1;
-	improper_type[m][num_improper[m]] = itype;
-	improper_atom1[m][num_improper[m]] = atom1;
-	improper_atom2[m][num_improper[m]] = atom2;
-	improper_atom3[m][num_improper[m]] = atom3;
-	improper_atom4[m][num_improper[m]] = atom4;
-	num_improper[m]++;
-	m = atom4-1;
-	improper_type[m][num_improper[m]] = itype;
-	improper_atom1[m][num_improper[m]] = atom1;
-	improper_atom2[m][num_improper[m]] = atom2;
-	improper_atom3[m][num_improper[m]] = atom3;
-	improper_atom4[m][num_improper[m]] = atom4;
-	num_improper[m]++;
+        m = atom1-1;
+        improper_type[m][num_improper[m]] = itype;
+        improper_atom1[m][num_improper[m]] = atom1;
+        improper_atom2[m][num_improper[m]] = atom2;
+        improper_atom3[m][num_improper[m]] = atom3;
+        improper_atom4[m][num_improper[m]] = atom4;
+        num_improper[m]++;
+        m = atom3-1;
+        improper_type[m][num_improper[m]] = itype;
+        improper_atom1[m][num_improper[m]] = atom1;
+        improper_atom2[m][num_improper[m]] = atom2;
+        improper_atom3[m][num_improper[m]] = atom3;
+        improper_atom4[m][num_improper[m]] = atom4;
+        num_improper[m]++;
+        m = atom4-1;
+        improper_type[m][num_improper[m]] = itype;
+        improper_atom1[m][num_improper[m]] = atom1;
+        improper_atom2[m][num_improper[m]] = atom2;
+        improper_atom3[m][num_improper[m]] = atom3;
+        improper_atom4[m][num_improper[m]] = atom4;
+        num_improper[m]++;
       }
     } else {
       count[atom2-1]++;
       if (newton_bond == 0) {
-	count[atom1-1]++;
-	count[atom3-1]++;
-	count[atom4-1]++;
+        count[atom1-1]++;
+        count[atom3-1]++;
+        count[atom4-1]++;
       }
     }
   }
@@ -1087,13 +1066,9 @@ void Molecule::nspecial_read(int flag, char *line)
 
   for (int i = 0; i < natoms; i++) {
     readline(line);
-    if (i == 0) {
-      int nwords = atom->count_words(line);
-      if (nwords != 4)
-        error->all(FLERR,"Invalid Special Bond Counts section in "
-                   "molecule file");
-    }
-    sscanf(line,"%d %d %d %d",&tmp,&c1,&c2,&c3);
+    if (4 != sscanf(line,"%d %d %d %d",&tmp,&c1,&c2,&c3))
+      error->all(FLERR,"Invalid Special Bond Counts section in "
+                 "molecule file");
 
     if (flag) {
       nspecial[i][0] = c1;
@@ -1117,13 +1092,13 @@ void Molecule::special_read(char *line)
     nwords = parse(line,words,maxspecial+1);
     if (nwords != nspecial[i][2]+1)
       error->all(FLERR,"Molecule file special list "
-		 "does not match special count");
+                 "does not match special count");
 
     for (m = 1; m < nwords; m++) {
       special[i][m-1] = ATOTAGINT(words[m]);
       if (special[i][m-1] <= 0 || special[i][m-1] > natoms ||
-	  special[i][m-1] == i+1)
-	error->all(FLERR,"Invalid special atom index in molecule file");
+          special[i][m-1] == i+1)
+        error->all(FLERR,"Invalid special atom index in molecule file");
     }
   }
 
@@ -1229,7 +1204,8 @@ void Molecule::shakeflag_read(char *line)
   int tmp;
   for (int i = 0; i < natoms; i++) {
     readline(line);
-    sscanf(line,"%d %d",&tmp,&shake_flag[i]);
+    if (2 != sscanf(line,"%d %d",&tmp,&shake_flag[i]))
+      error->all(FLERR,"Invalid Shake Flags section in molecule file");
   }
 
   for (int i = 0; i < natoms; i++)
@@ -1243,23 +1219,32 @@ void Molecule::shakeflag_read(char *line)
 
 void Molecule::shakeatom_read(char *line)
 {
-  int tmp;
+  int tmp, nmatch, nwant;
   for (int i = 0; i < natoms; i++) {
     readline(line);
-    if (shake_flag[i] == 1)
-      sscanf(line,"%d " TAGINT_FORMAT " " TAGINT_FORMAT " " TAGINT_FORMAT,
-             &tmp,&shake_atom[i][0],&shake_atom[i][1],&shake_atom[i][2]);
-    else if (shake_flag[i] == 2)
-      sscanf(line,"%d " TAGINT_FORMAT " " TAGINT_FORMAT,
-             &tmp,&shake_atom[i][0],&shake_atom[i][1]);
-    else if (shake_flag[i] == 3)
-      sscanf(line,"%d " TAGINT_FORMAT " " TAGINT_FORMAT " " TAGINT_FORMAT,
-             &tmp,&shake_atom[i][0],&shake_atom[i][1],&shake_atom[i][2]);
-    else if (shake_flag[i] == 4)
-      sscanf(line,"%d " TAGINT_FORMAT " " TAGINT_FORMAT " "
-             TAGINT_FORMAT " " TAGINT_FORMAT,
-             &tmp,&shake_atom[i][0],&shake_atom[i][1],
-             &shake_atom[i][2],&shake_atom[i][3]);
+    if (shake_flag[i] == 1) {
+      nmatch = sscanf(line,"%d " TAGINT_FORMAT " " TAGINT_FORMAT
+                      " " TAGINT_FORMAT,&tmp,&shake_atom[i][0],
+                      &shake_atom[i][1],&shake_atom[i][2]);
+      nwant = 4;
+    } else if (shake_flag[i] == 2) {
+      nmatch = sscanf(line,"%d " TAGINT_FORMAT " " TAGINT_FORMAT,
+                      &tmp,&shake_atom[i][0],&shake_atom[i][1]);
+      nwant = 3;
+    } else if (shake_flag[i] == 3) {
+      nmatch = sscanf(line,"%d " TAGINT_FORMAT " " TAGINT_FORMAT
+                      " " TAGINT_FORMAT,&tmp,&shake_atom[i][0],
+                      &shake_atom[i][1],&shake_atom[i][2]);
+      nwant = 4;
+    } else if (shake_flag[i] == 4) {
+      nmatch = sscanf(line,"%d " TAGINT_FORMAT " " TAGINT_FORMAT " "
+                      TAGINT_FORMAT " " TAGINT_FORMAT,
+                      &tmp,&shake_atom[i][0],&shake_atom[i][1],
+                      &shake_atom[i][2],&shake_atom[i][3]);
+      nwant = 5;
+    }
+    if (nmatch != nwant)
+      error->all(FLERR,"Invalid shake atom in molecule file");
   }
 
   for (int i = 0; i < natoms; i++) {
@@ -1277,19 +1262,27 @@ void Molecule::shakeatom_read(char *line)
 
 void Molecule::shaketype_read(char *line)
 {
-  int tmp;
+  int tmp,nmatch,nwant;
   for (int i = 0; i < natoms; i++) {
     readline(line);
-    if (shake_flag[i] == 1)
-      sscanf(line,"%d %d %d %d",&tmp,
-             &shake_type[i][0],&shake_type[i][1],&shake_type[i][2]);
-    else if (shake_flag[i] == 2)
-      sscanf(line,"%d %d",&tmp,&shake_type[i][0]);
-    else if (shake_flag[i] == 3)
-      sscanf(line,"%d %d %d",&tmp,&shake_type[i][0],&shake_type[i][1]);
-    else if (shake_flag[i] == 4)
-      sscanf(line,"%d %d %d %d",&tmp,
-             &shake_type[i][0],&shake_type[i][1],&shake_type[i][2]);
+    if (shake_flag[i] == 1) {
+      nmatch = sscanf(line,"%d %d %d %d",&tmp,&shake_type[i][0],
+                      &shake_type[i][1],&shake_type[i][2]);
+      nwant = 4;
+    } else if (shake_flag[i] == 2) {
+      nmatch = sscanf(line,"%d %d",&tmp,&shake_type[i][0]);
+      nwant = 2;
+    } else if (shake_flag[i] == 3) {
+      nmatch = sscanf(line,"%d %d %d",&tmp,&shake_type[i][0],
+                      &shake_type[i][1]);
+      nwant = 3;
+    } else if (shake_flag[i] == 4) {
+      nmatch = sscanf(line,"%d %d %d %d",&tmp,&shake_type[i][0],
+                      &shake_type[i][1],&shake_type[i][2]);
+      nwant = 4;
+    }
+    if (nmatch != nwant)
+      error->all(FLERR,"Invalid shake type data in molecule file");
   }
 
   for (int i = 0; i < natoms; i++) {
@@ -1501,46 +1494,46 @@ void Molecule::allocate()
 
   if (bondflag) {
     memory->create(bond_type,natoms,bond_per_atom,
-		   "molecule:bond_type");
+                   "molecule:bond_type");
     memory->create(bond_atom,natoms,bond_per_atom,
-		   "molecule:bond_atom");
+                   "molecule:bond_atom");
   }
 
   if (angleflag) {
     memory->create(angle_type,natoms,angle_per_atom,
-		   "molecule:angle_type");
+                   "molecule:angle_type");
     memory->create(angle_atom1,natoms,angle_per_atom,
-		   "molecule:angle_atom1");
+                   "molecule:angle_atom1");
     memory->create(angle_atom2,natoms,angle_per_atom,
-		   "molecule:angle_atom2");
+                   "molecule:angle_atom2");
     memory->create(angle_atom3,natoms,angle_per_atom,
-		   "molecule:angle_atom3");
+                   "molecule:angle_atom3");
   }
 
   if (dihedralflag) {
     memory->create(dihedral_type,natoms,dihedral_per_atom,
-		   "molecule:dihedral_type");
+                   "molecule:dihedral_type");
     memory->create(dihedral_atom1,natoms,dihedral_per_atom,
-		   "molecule:dihedral_atom1");
+                   "molecule:dihedral_atom1");
     memory->create(dihedral_atom2,natoms,dihedral_per_atom,
-		   "molecule:dihedral_atom2");
+                   "molecule:dihedral_atom2");
     memory->create(dihedral_atom3,natoms,dihedral_per_atom,
-		   "molecule:dihedral_atom3");
+                   "molecule:dihedral_atom3");
     memory->create(dihedral_atom4,natoms,dihedral_per_atom,
-		   "molecule:dihedral_atom4");
+                   "molecule:dihedral_atom4");
   }
 
   if (improperflag) {
     memory->create(improper_type,natoms,improper_per_atom,
-		   "molecule:improper_type");
+                   "molecule:improper_type");
     memory->create(improper_atom1,natoms,improper_per_atom,
-		   "molecule:improper_atom1");
+                   "molecule:improper_atom1");
     memory->create(improper_atom2,natoms,improper_per_atom,
-		   "molecule:improper_atom2");
+                   "molecule:improper_atom2");
     memory->create(improper_atom3,natoms,improper_per_atom,
-		   "molecule:improper_atom3");
+                   "molecule:improper_atom3");
     memory->create(improper_atom4,natoms,improper_per_atom,
-		   "molecule:improper_atom4");
+                   "molecule:improper_atom4");
   }
 
   if (shakeflag) {
@@ -1653,7 +1646,7 @@ void Molecule::parse_keyword(int flag, char *line, char *keyword)
     if (me == 0) {
       if (fgets(line,MAXLINE,fp) == NULL) eof = 1;
       while (eof == 0 && strspn(line," \t\n\r") == strlen(line)) {
-	if (fgets(line,MAXLINE,fp) == NULL) eof = 1;
+        if (fgets(line,MAXLINE,fp) == NULL) eof = 1;
       }
       if (fgets(keyword,MAXLINE,fp) == NULL) eof = 1;
     }
-- 
GitLab