Skip to content

Commit

Permalink
Octal literal fix in YAML parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
aligungr committed Jul 1, 2021
1 parent 8f55d2c commit e60b4cb
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/utils/yaml_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@
#include "yaml_utils.hpp"
#include "common.hpp"

#include <cctype>
#include <stdexcept>

#include <yaml-cpp/yaml.h>

namespace yaml
{

static YAML::Node OctalFix(const YAML::Node &node)
{
auto s = node.as<std::string>();

if (!std::all_of(s.begin(), s.end(), ::isdigit))
return node;
if (!s.empty() && s[0] != '0')
return node;

s.erase(0, std::min(s.find_first_not_of('0'), s.size() - 1));
return YAML::Node{s};
}

[[noreturn]] void FieldError(const std::string &name, const std::string &error)
{
throw std::runtime_error("Field '" + name + "' " + error + ".");
Expand All @@ -40,12 +54,8 @@ void AssertHasFields(const YAML::Node &node, const std::vector<std::string> &fie

int GetInt32(const YAML::Node &node, const std::string &name)
{
auto nodeValue = node[name].Scalar();
nodeValue.erase(0, std::min(nodeValue.find_first_not_of('0'), nodeValue.size()-1));
YAML::Node modifiedNode;
modifiedNode[name] = nodeValue;
AssertHasInt32(modifiedNode, name);
return modifiedNode[name].as<int>();
AssertHasInt32(node, name);
return OctalFix(node[name]).as<int>();
}

int GetInt32(const YAML::Node &node, const std::string &name, std::optional<int> minValue, std::optional<int> maxValue)
Expand All @@ -63,7 +73,7 @@ void AssertHasInt32(const YAML::Node &node, const std::string &name)
AssertHasField(node, name);
try
{
node[name].as<int>();
OctalFix(node[name]).as<int>();
}
catch (const std::runtime_error &e)
{
Expand Down Expand Up @@ -106,7 +116,7 @@ void AssertHasInt64(const YAML::Node &node, const std::string &name)
AssertHasField(node, name);
try
{
node[name].as<int64_t>();
OctalFix(node[name]).as<int64_t>();
}
catch (const std::runtime_error &e)
{
Expand All @@ -117,7 +127,7 @@ void AssertHasInt64(const YAML::Node &node, const std::string &name)
int64_t GetInt64(const YAML::Node &node, const std::string &name)
{
AssertHasInt64(node, name);
return node[name].as<int64_t>();
return OctalFix(node[name]).as<int64_t>();
}

int64_t GetInt64(const YAML::Node &node, const std::string &name, std::optional<int64_t> minValue,
Expand Down

0 comments on commit e60b4cb

Please sign in to comment.