From e9b4ab7363411960a199669291a88fc2d7dbb87a Mon Sep 17 00:00:00 2001 From: mkanski Date: Wed, 5 Jun 2019 16:12:24 +0200 Subject: [PATCH 1/2] Fixed potential file parsing when NULL or only some elements are used. --- src/USER-MISC/pair_extep.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/USER-MISC/pair_extep.cpp b/src/USER-MISC/pair_extep.cpp index 132b857dde..0ca775d552 100644 --- a/src/USER-MISC/pair_extep.cpp +++ b/src/USER-MISC/pair_extep.cpp @@ -653,15 +653,15 @@ void PairExTeP::read_file(char *file) // if all 3 args are in element list, then parse this line // else skip to next line - for (ielement = 0; ielement < nelements; ielement++) + for (ielement = 0; ielement < atom->ntypes; ielement++) if (strcmp(words[0],elements[ielement]) == 0) break; - if (ielement == nelements) continue; - for (jelement = 0; jelement < nelements; jelement++) + if (ielement == atom->ntypes) continue; + for (jelement = 0; jelement < atom->ntypes; jelement++) if (strcmp(words[1],elements[jelement]) == 0) break; - if (jelement == nelements) continue; - for (kelement = 0; kelement < nelements; kelement++) + if (jelement == atom->ntypes) continue; + for (kelement = 0; kelement < atom->ntypes; kelement++) if (strcmp(words[2],elements[kelement]) == 0) break; - if (kelement == nelements) continue; + if (kelement == atom->ntypes) continue; // load up parameter settings and error check their values @@ -705,7 +705,7 @@ void PairExTeP::read_file(char *file) error->all(FLERR,"Illegal ExTeP parameter"); nparams++; - if (nparams >= pow(atom->ntypes,3)) break; + if (nparams >= pow(nelements,3)) break; } // deallocate words array @@ -746,6 +746,9 @@ void PairExTeP::read_file(char *file) nwords = atom->count_words(line); if (nwords == 0) continue; + //skip a line if it is an unused single entry + if (nwords == 17) continue; + if (nwords != params_per_line) error->all(FLERR,"Incorrect format in ExTeP potential file"); @@ -761,12 +764,12 @@ void PairExTeP::read_file(char *file) // these lines set ielement and jelement to the // integers matching the strings from the input - for (ielement = 0; ielement < nelements; ielement++) + for (ielement = 0; ielement < atom->ntypes; ielement++) if (strcmp(words[0],elements[ielement]) == 0) break; - if (ielement == nelements) continue; - for (jelement = 0; jelement < nelements; jelement++) + if (ielement == atom->ntypes) continue; + for (jelement = 0; jelement < atom->ntypes; jelement++) if (strcmp(words[1],elements[jelement]) == 0) break; - if (jelement == nelements) continue; + if (jelement == atom->ntypes) continue; int Ni = atoi(words[2]); int Nj = atoi(words[3]); From 40a2f275c2aec8ff3585952f51e6b356fb1bc81c Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Wed, 5 Jun 2019 17:11:53 -0400 Subject: [PATCH 2/2] Fix a few more issues resulting from "nelements != atom->types" this also detects in a more safely fashion, whether there is data in the second part of the input file, that is still formatted for he first part. --- src/USER-MISC/pair_extep.cpp | 46 +++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/USER-MISC/pair_extep.cpp b/src/USER-MISC/pair_extep.cpp index 0ca775d552..bd5da71f4a 100644 --- a/src/USER-MISC/pair_extep.cpp +++ b/src/USER-MISC/pair_extep.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "pair_extep.h" #include "atom.h" #include "neighbor.h" @@ -653,15 +654,15 @@ void PairExTeP::read_file(char *file) // if all 3 args are in element list, then parse this line // else skip to next line - for (ielement = 0; ielement < atom->ntypes; ielement++) + for (ielement = 0; ielement < nelements; ielement++) if (strcmp(words[0],elements[ielement]) == 0) break; - if (ielement == atom->ntypes) continue; - for (jelement = 0; jelement < atom->ntypes; jelement++) + if (ielement == nelements) continue; + for (jelement = 0; jelement < nelements; jelement++) if (strcmp(words[1],elements[jelement]) == 0) break; - if (jelement == atom->ntypes) continue; - for (kelement = 0; kelement < atom->ntypes; kelement++) + if (jelement == nelements) continue; + for (kelement = 0; kelement < nelements; kelement++) if (strcmp(words[2],elements[kelement]) == 0) break; - if (kelement == atom->ntypes) continue; + if (kelement == nelements) continue; // load up parameter settings and error check their values @@ -746,17 +747,24 @@ void PairExTeP::read_file(char *file) nwords = atom->count_words(line); if (nwords == 0) continue; - //skip a line if it is an unused single entry - if (nwords == 17) continue; - - if (nwords != params_per_line) - error->all(FLERR,"Incorrect format in ExTeP potential file"); - // words = ptrs to all words in line nwords = 0; words[nwords++] = strtok(line," \t\n\r\f"); - while ((words[nwords++] = strtok(NULL," \t\n\r\f"))) continue; + while ((nwords < params_per_line) + && (words[nwords++] = strtok(NULL," \t\n\r\f"))) continue; + + // skip line if it is a leftover from the previous section, + // which can be identified by having 3 elements (instead of 2) + // as first words. + + if (isupper(words[0][0]) && isupper(words[1][0]) && isupper(words[2][0])) + continue; + + // need to have two elements followed by a number in each line + if (!(isupper(words[0][0]) && isupper(words[1][0]) + && !isupper(words[2][0]))) + error->all(FLERR,"Incorrect format in ExTeP potential file"); // ielement,jelement = 1st args // if all 3 args are in element list, then parse this line @@ -764,12 +772,12 @@ void PairExTeP::read_file(char *file) // these lines set ielement and jelement to the // integers matching the strings from the input - for (ielement = 0; ielement < atom->ntypes; ielement++) + for (ielement = 0; ielement < nelements; ielement++) if (strcmp(words[0],elements[ielement]) == 0) break; - if (ielement == atom->ntypes) continue; - for (jelement = 0; jelement < atom->ntypes; jelement++) + if (ielement == nelements) continue; + for (jelement = 0; jelement < nelements; jelement++) if (strcmp(words[1],elements[jelement]) == 0) break; - if (jelement == atom->ntypes) continue; + if (jelement == nelements) continue; int Ni = atoi(words[2]); int Nj = atoi(words[3]); @@ -1077,8 +1085,8 @@ void PairExTeP::costheta_d(double *rij_hat, double rij, // initialize spline for F_corr (based on PairLCBOP::F_conj) void PairExTeP::spline_init() { - for ( int iel=0; ielntypes; iel++) { - for ( int jel=0; jelntypes; jel++) { + for ( int iel=0; iel