jbeder/yaml-cpp

YAML octal scalars not converted to ints

matthew-eads opened this issue · 3 comments

The 1.2.2 YAML spec indicates octal literals should be formatted as "0o123" which is annoyingly not consistent with what C++ thinks octal strings should look like (0123).
The stringstream extraction currently used expects octals to have a single leading '0', not '0o'. This is used by yaml-cpp:

template <typename T>
typename std::enable_if<(std::is_same<T, unsigned char>::value ||
std::is_same<T, signed char>::value), bool>::type
ConvertStreamTo(std::stringstream& stream, T& rhs) {
int num;
if ((stream >> std::noskipws >> num) && (stream >> std::ws).eof()) {
if (num >= (std::numeric_limits<T>::min)() &&
num <= (std::numeric_limits<T>::max)()) {
rhs = static_cast<T>(num);
return true;
}
}
return false;
}

Which ultimately means "0o123" cannot be converted to an int. "0123" on the other hand can be converted to an int, which is maybe not the desired behavior if strictly adhering to the 1.2 spec.
Note this is true only as of YAML 1.2, version 1.1 specifies octals the opposite way 🙃.
Somewhat related to treatment of booleans: #1198

I think you're right. Probably the right behavior is to add the 0o123 format but not remove the 0123 format, since the YAML schema is allowed to be extended, and 0123 is reasonable to parse as octal.

but not remove the 0123 format, since the YAML schema is allowed to be extended, and 0123 is reasonable to parse as octal.

According to 1.2 specs, an old format is supposed to be parsed as decimal numbers:

Octal values need a 0o prefix; e.g. 010 is now parsed with the value 10 rather than 8.

Only a minority of YAML users are talking C. The others are completely unprepared for 010 = 8. Upstream YAML was right to correct the specs.