improve detection of invalid format strings. allow to replace format style variables

This commit is contained in:
Axel Kohlmeyer
2022-09-30 09:57:42 -04:00
parent fce0877cac
commit ec0bc5aa5e

View File

@ -389,19 +389,32 @@ void Variable::set(int narg, char **arg)
// 3rd is filled on retrieval // 3rd is filled on retrieval
} else if (strcmp(arg[1],"format") == 0) { } else if (strcmp(arg[1],"format") == 0) {
constexpr char validfmt[] = "% ?-?[0-9]*\\.?[0-9]*[efgEFG]";
if (narg != 4) error->all(FLERR,"Illegal variable command: expected 4 arguments but found {}", narg); if (narg != 4) error->all(FLERR,"Illegal variable command: expected 4 arguments but found {}", narg);
if (find(arg[0]) >= 0) return; int ivar = find(arg[0]);
if (nvar == maxvar) grow(); if (ivar >= 0) {
style[nvar] = FORMAT; if (style[ivar] != FORMAT)
num[nvar] = 3; error->all(FLERR,"Cannot redefine variable as a different style");
which[nvar] = 0; if (!utils::strmatch(arg[3], validfmt))
pad[nvar] = 0; error->all(FLERR,"Incorrect conversion in format string");
if (!utils::strmatch(arg[3],"%[0-9 ]*\\.[0-9]+[efgEFG]")) delete[] data[ivar][0];
error->all(FLERR,"Incorrect conversion in format string"); delete[] data[ivar][1];
data[nvar] = new char*[num[nvar]]; data[ivar][0] = utils::strdup(arg[2]);
copy(2,&arg[2],data[nvar]); data[ivar][1] = utils::strdup(arg[3]);
data[nvar][2] = new char[VALUELENGTH]; replaceflag = 1;
strcpy(data[nvar][2],"(undefined)"); } else {
if (nvar == maxvar) grow();
style[nvar] = FORMAT;
num[nvar] = 3;
which[nvar] = 0;
pad[nvar] = 0;
if (!utils::strmatch(arg[3], validfmt))
error->all(FLERR,"Incorrect conversion in format string");
data[nvar] = new char*[num[nvar]];
copy(2,&arg[2],data[nvar]);
data[nvar][2] = new char[VALUELENGTH];
strcpy(data[nvar][2],"(undefined)");
}
// EQUAL // EQUAL
// replace pre-existing var if also style EQUAL (allows it to be reset) // replace pre-existing var if also style EQUAL (allows it to be reset)