简体   繁体   中英

No exception thrown from boost::asio::ip::address::from_string on invalid address

Does anybody know why:

boost::system::error_code ec;
boost::asio::ip::address const addr =
   boost::asio::ip::address::from_string("10.10.10", ec);

does not return error, since 10.10.10 is invalid address ?

Instead of this 10.10.10 is modified to 10.10.0.10 in addr and no exception is thrown ?

boost::asio::ip::address::from_string() relies on inet_pton() internally which accepts things like 10.10.10 as valid addresses. The string "10.10" is used for the two top octets. The remaining "10" is interpreted as 16-bit number and splitted across the last two octets. Altogether this gets correctly interpreted into 10.10.0.10 .

Besides, your call boost::asio::ip::address::from_string("10.10.10", ec); does not throw since it uses boost::system::error_code .

Boost provide two type of variant for(Atlest which I uses) all function

  1. Which can two exception. This type of function doesn't have argument of type boost::system::error_code .
  2. Which shouldn't throw exception but instead it set parameter of type boost::system::error_code as argument accordingly.

Have look at https://wandbox.org/permlink/0wXerng3CYzhAOtT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM