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

Boost 1.87.0: Construction of io_context throws std::out_of_range if errno=34 before call #1588

Open
rndx21033 opened this issue Jan 18, 2025 · 1 comment

Comments

@rndx21033
Copy link

Boost 1.87.0: Construction of io_context throws std::out_of_range if errno=34 before call:


#include
#include <boost/asio/io_context.hpp>
#include

int main()
{
errno=34; // this will result in the following call throwing an exception
boost::asio::io_context ctx(std::thread::hardware_concurrency());
}

The offending code is in "asio/impl/config.hpp". Calls to strtoull and strtoll are followed by a check for errno==ERANGE, but there is not check first that ensures that the errno is actually relevant and has been set by that call.

Due to ambiguity of whether a return of LONG_MIN/LLONG_MIN/LONG_MAX/LLONG_MAX is due to that value being parsed or due to under/overflow, it seems like the safest course is to set errno=0 before those calls. I have tested this and confirmed that it does avoid the issue.

@psalinge
Copy link

psalinge commented Jan 24, 2025

We hit the same issue - together with #1587
Please find proposed patch in #1587

Clearing of errno is suggested by POSIX.1-2024 / The Open Group Base Specifications:

strtoul, strtoull — convert a string to an unsigned long
...
These functions shall not change the setting of errno if successful.

Since 0, {ULONG_MAX}, and {ULLONG_MAX} are returned on error and are also valid returns on success, an application wishing to check for error situations should set errno to 0, then call strtoul() or strtoull(), then check errno.

See also
https://pubs.opengroup.org/onlinepubs/9799919799/functions/strtoul.html
https://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10
https://en.cppreference.com/w/cpp/types/numeric_limits/digits10

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

2 participants