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

@ -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,
&tmp,&itype,&atom1,&atom2);
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,
&tmp,&itype,&atom1,&atom2,&atom3);
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,
&tmp,&itype,&atom1,&atom2,&atom3,&atom4);
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 "
TAGINT_FORMAT " " TAGINT_FORMAT " " TAGINT_FORMAT " " TAGINT_FORMAT,
&tmp,&itype,&atom1,&atom2,&atom3,&atom4);
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;