chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.72k stars 1.19k forks source link

asio::error::ssl_errors should be scoped to prevent UB #1473

Open anarthal opened 2 months ago

anarthal commented 2 months ago

The enumeration type is defined as:

enum ssl_errors
{
  // Error numbers are those produced by openssl.
};
inline boost::system::error_code make_error_code(ssl_errors e);

I get that errors belonging to this category are really OpenSSL error codes.

Now, I wrote something like this in my unit tests:

error_code ec {static_cast<asio::error::ssl_errors>(1623)}; // 1623 simulates an OpenSSL error

While this builds, this is flagged as undefined behavior by ubsan, as 1623 is not a valid enumerator. This wouldn't happen if ssl_errors would have been an enum class, instead.

Since ssl_errors doesn't have any enumerator, it looks like any such casts is a recipe for UB. I think the right thing to do would be converting the type to an enum class.

anarthal commented 2 months ago

Another viable solution is to explicitly state the underlying type of the enumeration, as in enum ssl_errors : unsigned {} (or whatever int type openssl uses)