Skip to content

Commit

Permalink
Improve unsigned integer check in xmlreader getValue
Browse files Browse the repository at this point in the history
Make checking for non negative numbers in tags robust against white spaces.

Signed-off-by: Christoph Niethammer <[email protected]>
  • Loading branch information
cniethammer committed Nov 16, 2023
1 parent 0497c06 commit 575adf0
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils/xmlfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,15 @@ template<typename T> bool XMLfile::Node::getValue(T& value) const
if(m_xmlnode)
{
std::stringstream ss(m_xmlnode->value());
ss >> value;
// Check if input has correct sign
if (std::is_unsigned_v<T>) {
if (ss.str().at(0) == '-') {
if (ss.str().find_first_of("-") != std::string::npos) {
std::cerr << "ERROR parsing \"" << ss.str() << "\" to data type " << typeid(T).name() << " from tag \"<" << name() << ">\" in xml file" << std::endl;
std::cerr << "The tag contains a negative value but an unsigned value was expected." << std::endl;
Simulation::exit(1);
}
}
ss >> value;
// Check if the entire string was consumed
if (!ss.eof() || ss.fail()) {
std::cerr << "ERROR parsing all chars of \"" << ss.str() << "\" from tag \"<" << name() << ">\" in xml file" << std::endl;
Expand Down

0 comments on commit 575adf0

Please sign in to comment.