Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAML octal scalars not converted to ints #1251

Open
matthew-eads opened this issue Dec 6, 2023 · 3 comments
Open

YAML octal scalars not converted to ints #1251

matthew-eads opened this issue Dec 6, 2023 · 3 comments

Comments

@matthew-eads
Copy link

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

@jbeder
Copy link
Owner

jbeder commented Jan 28, 2024

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.

@akortunov
Copy link

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.

@jwuttke
Copy link

jwuttke commented May 7, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants