Skip to content

Commit

Permalink
Merge pull request RobotLocomotion#3877 from jwnimmer-tri/3852-polyno…
Browse files Browse the repository at this point in the history
…mial-fail-fast

Avoid huge-integer accidents on invalid input
  • Loading branch information
jwnimmer-tri authored Oct 22, 2016
2 parents ffa1116 + 40bf97f commit d50fa4d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions drake/common/polynomial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <set>
#include <stdexcept>

#include "drake/common/drake_assert.h"
#include "drake/common/drake_throw.h"

using Eigen::Dynamic;
using Eigen::Matrix;
using Eigen::PolynomialSolver;
Expand Down Expand Up @@ -528,11 +531,13 @@ template <typename CoefficientType>
typename Polynomial<CoefficientType>::VarType
Polynomial<CoefficientType>::VariableNameToId(const string name,
const unsigned int m) {
DRAKE_THROW_UNLESS(IsValidVariableName(name));
unsigned int multiplier = 1;
VarType name_part = 0;
for (int i = (int)name.size() - 1; i >= 0; i--) {
VarType offset = static_cast<VarType>(
strchr(kNameChars, name[i]) - kNameChars);
for (int i = static_cast<int>(name.size()) - 1; i >= 0; i--) {
const char* const character_match = strchr(kNameChars, name[i]);
DRAKE_ASSERT(character_match);
VarType offset = static_cast<VarType>(character_match - kNameChars);
name_part += (offset + 1) * multiplier;
multiplier *= kNumNameChars + 1;
}
Expand Down

0 comments on commit d50fa4d

Please sign in to comment.