facebook / folly

An open-source C++ library developed and used at Facebook.
https://groups.google.com/forum/?fromgroups#!forum/facebook-folly
Apache License 2.0
28.47k stars 5.57k forks source link

Unhandled exception in `handleRead() noexcept` #1543

Open FatsharkGo opened 3 years ago

FatsharkGo commented 3 years ago

Function AsyncUDPSocket::handleRead() is specified as noexcept, it calls void SocketAddress::setFromSockaddr(const struct sockaddr* address, socklen_t addrlen) which will throw exception std::invalid_argument, but the function call does not in any catch block, std::terminate will be called if a exception is threw out. A tested fix is:

index f2fbe8c3c..91beff958 100644
--- a/folly/io/async/AsyncUDPSocket.cpp
+++ b/folly/io/async/AsyncUDPSocket.cpp
@@ -1028,7 +1028,17 @@ void AsyncUDPSocket::handleRead() noexcept {
     bytesRead = netops::recvfrom(fd_, buf, len, MSG_TRUNC, rawAddr, &addrLen);
 #endif
     if (bytesRead >= 0) {
-      clientAddress_.setFromSockaddr(rawAddr, addrLen);
+      try {
+        clientAddress_.setFromSockaddr(rawAddr, addrLen);
+      } catch (const std::exception &e) {
+        AsyncSocketException ex(AsyncSocketException::BAD_ARGS,
+                                "rawAddr invalid", errno);
+        auto cob = readCallback_;
+        readCallback_ = nullptr;
+        cob->onReadError(ex);
+        updateRegistration();
+        return;
+      }

       if (bytesRead > 0) {
         bool truncated = false;
yfeldblum commented 3 years ago

Under what valid (i.e., IPv4/IPv6) scenarios might this throw in practice?

FatsharkGo commented 3 years ago

It was running on Android and threw on a bounch of systems:

We have no idea about the scenario as the crash report and telemetry did not collect any data of this.