diff --git a/src/force.cpp b/src/force.cpp index cef61fc557..44c369acca 100644 --- a/src/force.cpp +++ b/src/force.cpp @@ -747,7 +747,14 @@ void Force::boundsbig(char *str, bigint nmax, bigint &nlo, bigint &nhi, double Force::numeric(const char *file, int line, char *str) { + if (!str) + error->all(file,line,"Expected floating point parameter " + "in input script or data file"); int n = strlen(str); + if (n == 0) + error->all(file,line,"Expected floating point parameter " + "in input script or data file"); + for (int i = 0; i < n; i++) { if (isdigit(str[i])) continue; if (str[i] == '-' || str[i] == '+' || str[i] == '.') continue; @@ -767,7 +774,14 @@ double Force::numeric(const char *file, int line, char *str) int Force::inumeric(const char *file, int line, char *str) { + if (!str) + error->all(file,line, + "Expected integer parameter in input script or data file"); int n = strlen(str); + if (n == 0) + error->all(file,line, + "Expected integer parameter in input script or data file"); + for (int i = 0; i < n; i++) { if (isdigit(str[i]) || str[i] == '-' || str[i] == '+') continue; error->all(file,line, @@ -785,14 +799,46 @@ int Force::inumeric(const char *file, int line, char *str) bigint Force::bnumeric(const char *file, int line, char *str) { + if (!str) + error->all(file,line, + "Expected integer parameter in input script or data file"); int n = strlen(str); + if (n == 0) + error->all(file,line, + "Expected integer parameter in input script or data file"); + for (int i = 0; i < n; i++) { if (isdigit(str[i]) || str[i] == '-' || str[i] == '+') continue; error->all(file,line, "Expected integer parameter in input script or data file"); } - return ATOLL(str); + return ATOBIGINT(str); +} + +/* ---------------------------------------------------------------------- + read a tag integer value from a string + generate an error if not a legitimate integer value + called by various commands to check validity of their arguments +------------------------------------------------------------------------- */ + +tagint Force::tnumeric(const char *file, int line, char *str) +{ + if (!str) + error->all(file,line, + "Expected integer parameter in input script or data file"); + int n = strlen(str); + if (n == 0) + error->all(file,line, + "Expected integer parameter in input script or data file"); + + for (int i = 0; i < n; i++) { + if (isdigit(str[i]) || str[i] == '-' || str[i] == '+') continue; + error->all(file,line, + "Expected integer parameter in input script or data file"); + } + + return ATOTAGINT(str); } /* ---------------------------------------------------------------------- diff --git a/src/force.h b/src/force.h index a9efb08fbf..bf364f253b 100644 --- a/src/force.h +++ b/src/force.h @@ -105,6 +105,7 @@ class Force : protected Pointers { double numeric(const char *, int, char *); int inumeric(const char *, int, char *); bigint bnumeric(const char *, int, char *); + tagint tnumeric(const char *, int, char *); FILE *open_potential(const char *); const char *potname(const char *); diff --git a/src/group.cpp b/src/group.cpp index 4144108f24..3b494a0b42 100644 --- a/src/group.cpp +++ b/src/group.cpp @@ -198,18 +198,12 @@ void Group::assign(int narg, char **arg) else error->all(FLERR,"Illegal group command"); tagint bound1,bound2; - if (sizeof(tagint) == sizeof(smallint)) - bound1 = force->inumeric(FLERR,arg[3]); - else - bound1 = force->bnumeric(FLERR,arg[3]); + bound1 = force->tnumeric(FLERR,arg[3]); bound2 = -1; if (condition == BETWEEN) { if (narg != 5) error->all(FLERR,"Illegal group command"); - if (sizeof(tagint) == sizeof(smallint)) - bound2 = force->inumeric(FLERR,arg[4]); - else - bound2 = force->bnumeric(FLERR,arg[4]); + bound2 = force->tnumeric(FLERR,arg[4]); } else if (narg != 4) error->all(FLERR,"Illegal group command"); int *attribute = NULL; @@ -284,13 +278,15 @@ void Group::assign(int narg, char **arg) for (int iarg = 2; iarg < narg; iarg++) { if (strchr(arg[iarg],':')) { - start = ATOTAGINT(strtok(arg[iarg],":")); - stop = ATOTAGINT(strtok(NULL,":")); + ptr = strtok(arg[iarg],":"); + start = force->tnumeric(FLERR,ptr); + ptr = strtok(NULL,":"); + stop = force->tnumeric(FLERR,ptr); ptr = strtok(NULL,":"); - if (ptr) delta = ATOTAGINT(ptr); + if (ptr) delta = force->tnumeric(FLERR,ptr); else delta = 1; } else { - start = stop = ATOTAGINT(arg[iarg]); + start = stop = force->tnumeric(FLERR,arg[iarg]); delta = 1; }