Skip to content

Commit

Permalink
Added index bounds checking to lots of methods
Browse files Browse the repository at this point in the history
  • Loading branch information
peastman committed Jan 31, 2012
1 parent 12daada commit a566a07
Show file tree
Hide file tree
Showing 178 changed files with 374 additions and 200 deletions.
16 changes: 4 additions & 12 deletions openmmapi/include/openmm/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ class OPENMM_EXPORT System {
*
* @param index the index of the particle for which to get the mass
*/
double getParticleMass(int index) const {
return masses[index];
}
double getParticleMass(int index) const;
/**
* Set the mass (in atomic mass units) of a particle. If the mass is 0, Integrators will ignore
* the particle and not modify its position or velocity. This is most often
Expand All @@ -109,9 +107,7 @@ class OPENMM_EXPORT System {
* @param index the index of the particle for which to set the mass
* @param mass the mass of the particle
*/
void setParticleMass(int index, double mass) {
masses[index] = mass;
}
void setParticleMass(int index, double mass);
/**
* Set a particle to be a virtual site. The VirtualSite object should have
* been created on the heap with the "new" operator. The System takes over
Expand Down Expand Up @@ -195,17 +191,13 @@ class OPENMM_EXPORT System {
*
* @param index the index of the Force to get
*/
const Force& getForce(int index) const {
return *forces[index];
}
const Force& getForce(int index) const;
/**
* Get a writable reference to one of the Forces in this System.
*
* @param index the index of the Force to get
*/
Force& getForce(int index) {
return *forces[index];
}
Force& getForce(int index);
/**
* Get the default values of the vectors defining the axes of the periodic box (measured in nm). Any newly
* created Context will have its box vectors set to these. They will affect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2008 Stanford University and the Authors. *
* Portions copyright (c) 2008-2012 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
Expand All @@ -36,23 +36,15 @@
* This file provides a variety of macros useful in test cases.
*/

#include "openmm/OpenMMException.h"
#include <cmath>
#include <string>
#include <sstream>

void throwException(const char* file, int line, const std::string& details) {
std::string fn(file);
std::string::size_type pos = fn.find_last_of("/\\");
if (pos+1>=fn.size())
pos=0;
std::string filename(fn,(int)(pos+1),(int)(fn.size()-(pos+1)));
std::stringstream message;
message << "Assertion failure at "<<filename<<":"<<line;
if (details.size() > 0)
message << ". "<<details;
throw OpenMM::OpenMMException(message.str());
}
namespace OpenMM {

void throwException(const char* file, int line, const std::string& details);

} // namespace OpenMM

#define ASSERT(cond) {if (!(cond)) throwException(__FILE__, __LINE__, "");};

Expand All @@ -64,4 +56,6 @@ void throwException(const char* file, int line, const std::string& details) {

#define ASSERT_USUALLY_EQUAL_TOL(expected, found, tol) {double _scale_ = std::abs(expected) > 1.0 ? std::abs(expected) : 1.0; if (!(std::abs((expected)-(found))/_scale_ <= (tol))) {std::stringstream details; details << "Expected "<<(expected)<<", found "<<(found)<<" (This test is stochastic and may occasionally fail)"; throwException(__FILE__, __LINE__, details.str());}};

#define ASSERT_VALID_INDEX(index, vector) {if (index < 0 || index >= vector.size()) throwException(__FILE__, __LINE__, "Index out of range");};

#endif /*OPENMM_ASSERTIONUTILITIES_H_*/
56 changes: 56 additions & 0 deletions openmmapi/src/AssertionUtilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* -------------------------------------------------------------------------- *
* OpenMM *
* -------------------------------------------------------------------------- *
* This is part of the OpenMM molecular simulation toolkit originating from *
* Simbios, the NIH National Center for Physics-Based Simulation of *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2008-2012 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
* USE OR OTHER DEALINGS IN THE SOFTWARE. *
* -------------------------------------------------------------------------- */

/**
* This file provides a variety of macros useful in test cases.
*/

#include "openmm/internal/AssertionUtilities.h"
#include "openmm/OpenMMException.h"
#include <string>
#include <sstream>

namespace OpenMM {

void throwException(const char* file, int line, const std::string& details) {
std::string fn(file);
std::string::size_type pos = fn.find_last_of("/\\");
if (pos+1>=fn.size())
pos=0;
std::string filename(fn,(int)(pos+1),(int)(fn.size()-(pos+1)));
std::stringstream message;
message << "Assertion failure at "<<filename<<":"<<line;
if (details.size() > 0)
message << ". "<<details;
throw OpenMMException(message.str());
}

} // namespace OpenMM
5 changes: 5 additions & 0 deletions openmmapi/src/CMAPTorsionForce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "openmm/Force.h"
#include "openmm/OpenMMException.h"
#include "openmm/CMAPTorsionForce.h"
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/internal/CMAPTorsionForceImpl.h"

using namespace OpenMM;
Expand All @@ -47,11 +48,13 @@ int CMAPTorsionForce::addMap(int size, const std::vector<double>& energy) {
}

void CMAPTorsionForce::getMapParameters(int index, int& size, std::vector<double>& energy) const {
ASSERT_VALID_INDEX(index, maps);
size = maps[index].size;
energy = maps[index].energy;
}

void CMAPTorsionForce::setMapParameters(int index, int size, const std::vector<double>& energy) {
ASSERT_VALID_INDEX(index, maps);
if (energy.size() != size*size)
throw OpenMMException("CMAPTorsionForce: incorrect number of energy values");
maps[index].size = size;
Expand All @@ -64,6 +67,7 @@ int CMAPTorsionForce::addTorsion(int map, int a1, int a2, int a3, int a4, int b1
}

void CMAPTorsionForce::getTorsionParameters(int index, int& map, int& a1, int& a2, int& a3, int& a4, int& b1, int& b2, int& b3, int& b4) const {
ASSERT_VALID_INDEX(index, torsions);
map = torsions[index].map;
a1 = torsions[index].a1;
a2 = torsions[index].a2;
Expand All @@ -76,6 +80,7 @@ void CMAPTorsionForce::getTorsionParameters(int index, int& map, int& a1, int& a
}

void CMAPTorsionForce::setTorsionParameters(int index, int map, int a1, int a2, int a3, int a4, int b1, int b2, int b3, int b4) {
ASSERT_VALID_INDEX(index, torsions);
torsions[index].map = map;
torsions[index].a1 = a1;
torsions[index].a2 = a2;
Expand Down
9 changes: 9 additions & 0 deletions openmmapi/src/CustomAngleForce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "openmm/Force.h"
#include "openmm/OpenMMException.h"
#include "openmm/CustomAngleForce.h"
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/internal/CustomAngleForceImpl.h"
#include <cmath>
#include <map>
Expand Down Expand Up @@ -60,10 +61,12 @@ int CustomAngleForce::addPerAngleParameter(const string& name) {
}

const string& CustomAngleForce::getPerAngleParameterName(int index) const {
ASSERT_VALID_INDEX(index, parameters);
return parameters[index].name;
}

void CustomAngleForce::setPerAngleParameterName(int index, const string& name) {
ASSERT_VALID_INDEX(index, parameters);
parameters[index].name = name;
}

Expand All @@ -73,18 +76,22 @@ int CustomAngleForce::addGlobalParameter(const string& name, double defaultValue
}

const string& CustomAngleForce::getGlobalParameterName(int index) const {
ASSERT_VALID_INDEX(index, globalParameters);
return globalParameters[index].name;
}

void CustomAngleForce::setGlobalParameterName(int index, const string& name) {
ASSERT_VALID_INDEX(index, globalParameters);
globalParameters[index].name = name;
}

double CustomAngleForce::getGlobalParameterDefaultValue(int index) const {
ASSERT_VALID_INDEX(index, globalParameters);
return globalParameters[index].defaultValue;
}

void CustomAngleForce::setGlobalParameterDefaultValue(int index, double defaultValue) {
ASSERT_VALID_INDEX(index, globalParameters);
globalParameters[index].defaultValue = defaultValue;
}

Expand All @@ -94,13 +101,15 @@ int CustomAngleForce::addAngle(int particle1, int particle2, int particle3, cons
}

void CustomAngleForce::getAngleParameters(int index, int& particle1, int& particle2, int& particle3, std::vector<double>& parameters) const {
ASSERT_VALID_INDEX(index, angles);
particle1 = angles[index].particle1;
particle2 = angles[index].particle2;
particle3 = angles[index].particle3;
parameters = angles[index].parameters;
}

void CustomAngleForce::setAngleParameters(int index, int particle1, int particle2, int particle3, const vector<double>& parameters) {
ASSERT_VALID_INDEX(index, angles);
angles[index].parameters = parameters;
angles[index].particle1 = particle1;
angles[index].particle2 = particle2;
Expand Down
9 changes: 9 additions & 0 deletions openmmapi/src/CustomBondForce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "openmm/Force.h"
#include "openmm/OpenMMException.h"
#include "openmm/CustomBondForce.h"
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/internal/CustomBondForceImpl.h"
#include <cmath>
#include <map>
Expand Down Expand Up @@ -60,10 +61,12 @@ int CustomBondForce::addPerBondParameter(const string& name) {
}

const string& CustomBondForce::getPerBondParameterName(int index) const {
ASSERT_VALID_INDEX(index, parameters);
return parameters[index].name;
}

void CustomBondForce::setPerBondParameterName(int index, const string& name) {
ASSERT_VALID_INDEX(index, parameters);
parameters[index].name = name;
}

Expand All @@ -73,18 +76,22 @@ int CustomBondForce::addGlobalParameter(const string& name, double defaultValue)
}

const string& CustomBondForce::getGlobalParameterName(int index) const {
ASSERT_VALID_INDEX(index, globalParameters);
return globalParameters[index].name;
}

void CustomBondForce::setGlobalParameterName(int index, const string& name) {
ASSERT_VALID_INDEX(index, globalParameters);
globalParameters[index].name = name;
}

double CustomBondForce::getGlobalParameterDefaultValue(int index) const {
ASSERT_VALID_INDEX(index, globalParameters);
return globalParameters[index].defaultValue;
}

void CustomBondForce::setGlobalParameterDefaultValue(int index, double defaultValue) {
ASSERT_VALID_INDEX(index, globalParameters);
globalParameters[index].defaultValue = defaultValue;
}

Expand All @@ -94,12 +101,14 @@ int CustomBondForce::addBond(int particle1, int particle2, const vector<double>&
}

void CustomBondForce::getBondParameters(int index, int& particle1, int& particle2, std::vector<double>& parameters) const {
ASSERT_VALID_INDEX(index, bonds);
particle1 = bonds[index].particle1;
particle2 = bonds[index].particle2;
parameters = bonds[index].parameters;
}

void CustomBondForce::setBondParameters(int index, int particle1, int particle2, const vector<double>& parameters) {
ASSERT_VALID_INDEX(index, bonds);
bonds[index].parameters = parameters;
bonds[index].particle1 = particle1;
bonds[index].particle2 = particle2;
Expand Down
9 changes: 9 additions & 0 deletions openmmapi/src/CustomExternalForce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "openmm/Force.h"
#include "openmm/OpenMMException.h"
#include "openmm/CustomExternalForce.h"
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/internal/CustomExternalForceImpl.h"
#include <cmath>
#include <map>
Expand Down Expand Up @@ -60,10 +61,12 @@ int CustomExternalForce::addPerParticleParameter(const string& name) {
}

const string& CustomExternalForce::getPerParticleParameterName(int index) const {
ASSERT_VALID_INDEX(index, parameters);
return parameters[index].name;
}

void CustomExternalForce::setPerParticleParameterName(int index, const string& name) {
ASSERT_VALID_INDEX(index, parameters);
parameters[index].name = name;
}

Expand All @@ -73,18 +76,22 @@ int CustomExternalForce::addGlobalParameter(const string& name, double defaultVa
}

const string& CustomExternalForce::getGlobalParameterName(int index) const {
ASSERT_VALID_INDEX(index, globalParameters);
return globalParameters[index].name;
}

void CustomExternalForce::setGlobalParameterName(int index, const string& name) {
ASSERT_VALID_INDEX(index, globalParameters);
globalParameters[index].name = name;
}

double CustomExternalForce::getGlobalParameterDefaultValue(int index) const {
ASSERT_VALID_INDEX(index, globalParameters);
return globalParameters[index].defaultValue;
}

void CustomExternalForce::setGlobalParameterDefaultValue(int index, double defaultValue) {
ASSERT_VALID_INDEX(index, globalParameters);
globalParameters[index].defaultValue = defaultValue;
}

Expand All @@ -94,11 +101,13 @@ int CustomExternalForce::addParticle(int particle, const vector<double>& paramet
}

void CustomExternalForce::getParticleParameters(int index, int& particle, std::vector<double>& parameters) const {
ASSERT_VALID_INDEX(index, particles);
particle = particles[index].particle;
parameters = particles[index].parameters;
}

void CustomExternalForce::setParticleParameters(int index, int particle, const vector<double>& parameters) {
ASSERT_VALID_INDEX(index, particles);
particles[index].parameters = parameters;
particles[index].particle = particle;
}
Expand Down
Loading

0 comments on commit a566a07

Please sign in to comment.