Skip to content

Commit

Permalink
parser: more robust handling of number syntax errors
Browse files Browse the repository at this point in the history
stoi and stod functions are quite permissive and can ignore part of the
input string leaving it not entirely parsed.  Sometimes this could
result in invalid number strings being parsed as valid.  For example
"3ee10" is parsed as the value "3".  This change fixes that and requires
sto[i/d] to parse the entire string.  Any corresponding errors are not
added to the _errmsg collection variable and printed together with
other errors all at once instead of one at a time.

fixes idaholab#10310
  • Loading branch information
rwcarlsen committed Nov 29, 2017
1 parent ab81b4b commit d92d068
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
36 changes: 23 additions & 13 deletions framework/src/parser/Parser.C
Expand Up @@ -332,11 +332,12 @@ Parser::walkRaw(std::string /*fullpath*/, std::string /*nodepath*/, hit::Node *

if (iters.first == iters.second)
{
mooseError(errormsg(getFileName(),
_errmsg += errormsg(getFileName(),
n,
"section '",
curr_identifier,
"' does not have an associated \"Action\".\nDid you misspell it?"));
"' does not have an associated \"Action\".\nDid you misspell it?");
return;
}

for (auto it = iters.first; it != iters.second; ++it)
Expand Down Expand Up @@ -531,9 +532,6 @@ Parser::parse(const std::string & input_filename)
for (auto & msg : bw.errors)
_errmsg += msg + "\n";

if (_errmsg.size() > 0)
mooseError(_errmsg);

// There are a few order dependent actions that have to be built first in
// order for the parser and application to function properly:
//
Expand Down Expand Up @@ -562,6 +560,9 @@ Parser::parse(const std::string & input_filename)
walkRaw(n->parent()->fullpath(), n->path(), n);
}
_root->walk(this, hit::NodeType::Section);

if (_errmsg.size() > 0)
mooseError(_errmsg);
}

// Checks the input and the way it has been used and emits any errors/warnings.
Expand Down Expand Up @@ -1250,6 +1251,9 @@ Parser::setScalarParameter(const std::string & full_name,
}
catch (hit::Error & err)
{
auto strval = _root->param<std::string>(full_name);
size_t pos = 0;

// handle the case where the user put a number inside quotes - we really shouldn't allow this,
// but "backwards compatibility" :-(
auto & t = typeid(T);
Expand All @@ -1258,34 +1262,40 @@ Parser::setScalarParameter(const std::string & full_name,
{
try
{
auto v = std::stoi(_root->param<std::string>(full_name));
auto v = std::stoi(strval, &pos);
param->set() = *reinterpret_cast<T *>(&v);
if (pos != strval.size())
throw std::runtime_error("dummy"); // catch below and emit error
}
catch (...)
catch (...) // some std::exception's were slipping through with "catch(std::exception&)"
{
mooseError(errormsg(_input_filename,
_errmsg += errormsg(_input_filename,
_root->find(full_name),
"invalid integer syntax for parameter: ",
full_name,
"=",
_root->param<std::string>(full_name)));
strval) +
"\n";
}
}
else if (t == typeid(double))
{
try
{
auto v = std::stod(_root->param<std::string>(full_name));
auto v = std::stod(strval, &pos);
param->set() = *reinterpret_cast<T *>(&v);
if (pos != strval.size())
throw std::runtime_error("dummy"); // catch below and emit error
}
catch (...)
catch (...) // some std::exception's were slipping through with "catch(std::exception&)"
{
mooseError(errormsg(_input_filename,
_errmsg += errormsg(_input_filename,
_root->find(full_name),
"invalid float syntax for parameter: ",
full_name,
"=",
_root->param<std::string>(full_name)));
strval) +
"\n";
}
}
else
Expand Down
43 changes: 43 additions & 0 deletions test/tests/misc/check_error/bad_number.i
@@ -0,0 +1,43 @@
[Mesh]
type = GeneratedMesh
dim = 1
nx = 10
[]

[Variables]
[./u]
[../]
[]

[Kernels]
[./diff]
type = Diffusion
variable = u
[../]
[]

[BCs]
[./left]
type = DirichletBC
variable = u
boundary = left
value = 0
[../]
[./right]
type = DirichletBC
variable = u
boundary = right
value = 1.2eE
[../]
[]

[Executioner]
type = Steady
solve_type = 'PJFNK'
petsc_options_iname = '-pc_type -pc_hypre_type'
petsc_options_value = 'hypre boomeramg'
[]

[Outputs]
exodus = true
[]
6 changes: 6 additions & 0 deletions test/tests/misc/check_error/tests
Expand Up @@ -558,4 +558,10 @@
expect_out = "Function name 'x' could evaluate as a ParsedFunction"
allow_warnings = true
[../]

[./bad_number]
type = 'RunException'
input = 'bad_number.i'
expect_err = "invalid float syntax for parameter: BCs/right/value"
[../]
[]

0 comments on commit d92d068

Please sign in to comment.