Skip to content

Commit

Permalink
More checks before using castToXX() functions
Browse files Browse the repository at this point in the history
  • Loading branch information
GiovanniBussi committed Apr 19, 2024
1 parent 885599e commit 7c8c98f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/cltools/Driver.cpp
Expand Up @@ -727,7 +727,9 @@ int Driver<real>::main(FILE* in,FILE*out,Communicator& pc) {
std::map<std::string,std::vector<std::string> > data;
IFile ifile; ifile.open(plumedFile); std::vector<std::string> words;
while( Tools::getParsedLine(ifile,words) && !p.getEndPlumed() ) {
p.readInputWords(words,false); Action* aa=p.getActionSet()[p.getActionSet().size()-1].get();
p.readInputWords(words,false);
Action* aa=p.getActionSet()[p.getActionSet().size()-1].get();
plumed_assert(aa); // needed for following calls, see #1046
ActionWithValue* av=aa->castToActionWithValue();
if( av && aa->getDefaultString().length()>0 ) {
std::vector<std::string> def; def.push_back( "defaults " + aa->getDefaultString() );
Expand Down
9 changes: 7 additions & 2 deletions src/core/ActionAtomistic.cpp
Expand Up @@ -53,6 +53,7 @@ ActionAtomistic::ActionAtomistic(const ActionOptions&ao):
// We now get all the information about atoms that are lying about
std::vector<ActionWithValue*> vatoms = plumed.getActionSet().select<ActionWithValue*>();
for(const auto & vv : vatoms ) {
plumed_assert(vv); // needed for following calls, see #1046
ActionToPutData* ap = vv->castToActionToPutData();
if( ap ) {
if( ap->getRole()=="x" ) xpos.push_back( ap->copyOutput(0) );
Expand Down Expand Up @@ -324,11 +325,15 @@ std::pair<std::size_t, std::size_t> ActionAtomistic::getValueIndices( const Atom

void ActionAtomistic::retrieveAtoms( const bool& force ) {
if( boxValue ) {
PbcAction* pbca = boxValue->getPntrToAction()->castToPbcAction();
auto* ptr=boxValue->getPntrToAction();
plumed_assert(ptr); // needed for following calls, see #1046
PbcAction* pbca = ptr->castToPbcAction();
plumed_assert( pbca ); pbc=pbca->pbc;
}
if( donotretrieve || indexes.size()==0 ) return;
ActionToPutData* cv = chargev[0]->getPntrToAction()->castToActionToPutData();
auto * ptr=chargev[0]->getPntrToAction();
plumed_assert(ptr); // needed for following calls, see #1046
ActionToPutData* cv = ptr->castToActionToPutData();
if(cv) chargesWereSet=cv->hasBeenSet();
unsigned j = 0;

Expand Down
4 changes: 3 additions & 1 deletion src/core/ActionToPutData.cpp
Expand Up @@ -160,7 +160,9 @@ void ActionToPutData::apply() {

unsigned ActionToPutData::getNumberOfForcesToRescale() const {
if( getName()!="ENERGY" || getDependencies().size()>0 ) return copyOutput(0)->getNumberOfValues();
plumed_assert( getDependencies().size()==1 ); ActionForInterface* ai = getDependencies()[0]->castToActionForInterface();
plumed_assert( getDependencies().size()==1 );
plumed_assert(getDependencies()[0]); // needed for following calls, see #1046
ActionForInterface* ai = getDependencies()[0]->castToActionForInterface();
return ai->getNumberOfForcesToRescale();
}

Expand Down
6 changes: 5 additions & 1 deletion src/core/ActionWithArguments.cpp
Expand Up @@ -107,6 +107,7 @@ void ActionWithArguments::interpretArgumentList(const std::vector<std::string>&
std::vector<ActionWithValue*> all=as.select<ActionWithValue*>();
if( all.empty() ) readact->error("your input file is not telling plumed to calculate anything");
for(unsigned j=0; j<all.size(); j++) {
plumed_assert(all[j]); // needed for following calls, see #1046
ActionForInterface* ap=all[j]->castToActionForInterface(); if( ap ) continue;
for(int k=0; k<all[j]->getNumberOfComponents(); ++k) arg.push_back(all[j]->copyOutput(k));
}
Expand Down Expand Up @@ -166,6 +167,7 @@ void ActionWithArguments::interpretArgumentList(const std::vector<std::string>&
std::vector<ActionWithValue*> all=as.select<ActionWithValue*>();
if( all.empty() ) readact->error("your input file is not telling plumed to calculate anything");
for(unsigned j=0; j<all.size(); j++) {
plumed_assert(all[j]); // needed for following calls, see #1046
ActionWithVirtualAtom* av=all[j]->castToActionWithVirtualAtom(); if( av ) continue;
ActionForInterface* ap=all[j]->castToActionForInterface(); if( ap && all[j]->getName()!="ENERGY" ) continue;
for(int k=0; k<all[j]->getNumberOfComponents(); ++k) arg.push_back(all[j]->copyOutput(k));
Expand Down Expand Up @@ -327,7 +329,9 @@ bool ActionWithArguments::calculateConstantValues( const bool& haveatoms ) {
if( !av || arguments.size()==0 ) return false;
bool constant = true, atoms=false;
for(unsigned i=0; i<arguments.size(); ++i) {
ActionAtomistic* aa=arguments[i]->getPntrToAction()->castToActionAtomistic();
auto * ptr=arguments[i]->getPntrToAction();
plumed_assert(ptr); // needed for following calls, see #1046
ActionAtomistic* aa=ptr->castToActionAtomistic();
if( aa ) {
ActionWithVector* av=dynamic_cast<ActionWithVector*>( arguments[i]->getPntrToAction() );
if( !av || aa->getNumberOfAtoms()>0 ) atoms=true;
Expand Down
10 changes: 10 additions & 0 deletions src/core/PlumedMain.cpp
Expand Up @@ -434,20 +434,23 @@ void PlumedMain::cmd(std::string_view word,const TypesafePtr & val) {
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
for(const auto & pp : inputs ) {
plumed_assert(pp);
DomainDecomposition* dd=pp->castToDomainDecomposition();
if( dd ) dd->setAtomsNlocal(val.get<int>());
}
break;
case cmd_setAtomsGatindex:
CHECK_INIT(initialized,word);
for(const auto & pp : inputs ) {
plumed_assert(pp);
DomainDecomposition* dd=pp->castToDomainDecomposition();
if( dd ) dd->setAtomsGatindex(val,false);
}
break;
case cmd_setAtomsFGatindex:
CHECK_INIT(initialized,word);
for(const auto & pp : inputs ) {
plumed_assert(pp);
DomainDecomposition* dd=pp->castToDomainDecomposition();
if( dd ) dd->setAtomsGatindex(val,false);
}
Expand All @@ -456,6 +459,7 @@ void PlumedMain::cmd(std::string_view word,const TypesafePtr & val) {
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
for(const auto & pp : inputs ) {
plumed_assert(pp);
DomainDecomposition* dd=pp->castToDomainDecomposition();
if( dd ) dd->setAtomsContiguous(val.get<int>());
}
Expand All @@ -464,6 +468,7 @@ void PlumedMain::cmd(std::string_view word,const TypesafePtr & val) {
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
for(const auto & pp : inputs ) {
plumed_assert(pp);
DomainDecomposition* dd=pp->castToDomainDecomposition();
if( dd ) dd->createFullList(val);
}
Expand All @@ -474,6 +479,7 @@ void PlumedMain::cmd(std::string_view word,const TypesafePtr & val) {
CHECK_NOTNULL(val,word);
unsigned nlists=0;
for(const auto & pp : inputs ) {
plumed_assert(pp);
DomainDecomposition* dd=pp->castToDomainDecomposition();
if( dd ) { dd->getFullList(val); nlists++; }
}
Expand All @@ -483,6 +489,7 @@ void PlumedMain::cmd(std::string_view word,const TypesafePtr & val) {
case cmd_clearFullList:
CHECK_INIT(initialized,word);
for(const auto & pp : inputs ) {
plumed_assert(pp);
DomainDecomposition* dd=pp->castToDomainDecomposition();
if( dd ) dd->clearFullList();
}
Expand Down Expand Up @@ -540,6 +547,7 @@ void PlumedMain::cmd(std::string_view word,const TypesafePtr & val) {
CHECK_INIT(initialized,word);
std::vector<int> natoms;
for(const auto & pp : inputs ) {
plumed_assert(pp);
DomainDecomposition* dd=pp->castToDomainDecomposition();
if ( dd ) natoms.push_back( dd->getNumberOfAtoms() );
}
Expand Down Expand Up @@ -873,6 +881,7 @@ void PlumedMain::init() {
log<<"Number of threads: "<<OpenMP::getNumThreads()<<"\n";
log<<"Cache line size: "<<OpenMP::getCachelineSize()<<"\n";
for(const auto & pp : inputs ) {
plumed_assert(pp);
DomainDecomposition* dd=pp->castToDomainDecomposition();
if ( dd ) log.printf("Number of atoms: %d\n",dd->getNumberOfAtoms());
}
Expand Down Expand Up @@ -1123,6 +1132,7 @@ void PlumedMain::justCalculate() {
// calculate the active actions in order (assuming *backward* dependence)
for(const auto & pp : actionSet) {
Action* p(pp.get());
plumed_assert(p);
try {
if(p->isActive()) {
// Stopwatch is stopped when sw goes out of scope.
Expand Down

1 comment on commit 7c8c98f

@PlumedBot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found broken examples in automatic/ANGLES.tmp
Found broken examples in automatic/ANN.tmp
Found broken examples in automatic/CAVITY.tmp
Found broken examples in automatic/CLASSICAL_MDS.tmp
Found broken examples in automatic/CLUSTER_DIAMETER.tmp
Found broken examples in automatic/CLUSTER_DISTRIBUTION.tmp
Found broken examples in automatic/CLUSTER_PROPERTIES.tmp
Found broken examples in automatic/CONSTANT.tmp
Found broken examples in automatic/CONTACT_MATRIX.tmp
Found broken examples in automatic/CONTACT_MATRIX_PROPER.tmp
Found broken examples in automatic/COORDINATIONNUMBER.tmp
Found broken examples in automatic/DFSCLUSTERING.tmp
Found broken examples in automatic/DISTANCE_FROM_CONTOUR.tmp
Found broken examples in automatic/EDS.tmp
Found broken examples in automatic/EMMI.tmp
Found broken examples in automatic/ENVIRONMENTSIMILARITY.tmp
Found broken examples in automatic/FIND_CONTOUR.tmp
Found broken examples in automatic/FIND_CONTOUR_SURFACE.tmp
Found broken examples in automatic/FIND_SPHERICAL_CONTOUR.tmp
Found broken examples in automatic/FOURIER_TRANSFORM.tmp
Found broken examples in automatic/FUNCPATHGENERAL.tmp
Found broken examples in automatic/FUNCPATHMSD.tmp
Found broken examples in automatic/FUNNEL.tmp
Found broken examples in automatic/FUNNEL_PS.tmp
Found broken examples in automatic/GHBFIX.tmp
Found broken examples in automatic/GPROPERTYMAP.tmp
Found broken examples in automatic/HBOND_MATRIX.tmp
Found broken examples in automatic/INCLUDE.tmp
Found broken examples in automatic/INCYLINDER.tmp
Found broken examples in automatic/INENVELOPE.tmp
Found broken examples in automatic/INTERPOLATE_GRID.tmp
Found broken examples in automatic/LOCAL_AVERAGE.tmp
Found broken examples in automatic/MAZE_OPTIMIZER_BIAS.tmp
Found broken examples in automatic/MAZE_RANDOM_ACCELERATION_MD.tmp
Found broken examples in automatic/MAZE_SIMULATED_ANNEALING.tmp
Found broken examples in automatic/MAZE_STEERED_MD.tmp
Found broken examples in automatic/MULTICOLVARDENS.tmp
Found broken examples in automatic/OUTPUT_CLUSTER.tmp
Found broken examples in automatic/PAMM.tmp
Found broken examples in automatic/PCA.tmp
Found broken examples in automatic/PCAVARS.tmp
Found broken examples in automatic/PIV.tmp
Found broken examples in automatic/PLUMED.tmp
Found broken examples in automatic/PYCVINTERFACE.tmp
Found broken examples in automatic/PYTHONFUNCTION.tmp
Found broken examples in automatic/Q3.tmp
Found broken examples in automatic/Q4.tmp
Found broken examples in automatic/Q6.tmp
Found broken examples in automatic/QUATERNION.tmp
Found broken examples in automatic/SPRINT.tmp
Found broken examples in automatic/TETRAHEDRALPORE.tmp
Found broken examples in automatic/TORSIONS.tmp
Found broken examples in automatic/WHAM_WEIGHTS.tmp
Found broken examples in AnalysisPP.md
Found broken examples in CollectiveVariablesPP.md
Found broken examples in MiscelaneousPP.md

Please sign in to comment.