Fix use after move in AsyncSSLSocket constructor.
authorKyle Nekritz <knekritz@fb.com>
Fri, 10 Mar 2017 17:36:52 +0000 (09:36 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 10 Mar 2017 17:50:15 +0000 (09:50 -0800)
Summary: The new AsyncSocket is already constructed at this point so just use the event base from it.

Reviewed By: djwatson

Differential Revision: D4689395

fbshipit-source-id: aac898c1c6e6e0e0ffcb20b16bbf2b842cc31d54

folly/io/async/AsyncSSLSocket.cpp
folly/io/async/test/AsyncSSLSocketTest.cpp

index 26090b7ba770dfa7bd762f46f19aa440a1769a00..3d16fc68307c1a839aebba987104b71f2830038b 100644 (file)
@@ -248,8 +248,8 @@ AsyncSSLSocket::AsyncSSLSocket(
     : AsyncSocket(std::move(oldAsyncSocket)),
       server_(server),
       ctx_(ctx),
-      handshakeTimeout_(this, oldAsyncSocket->getEventBase()),
-      connectionTimeout_(this, oldAsyncSocket->getEventBase()) {
+      handshakeTimeout_(this, AsyncSocket::getEventBase()),
+      connectionTimeout_(this, AsyncSocket::getEventBase()) {
   noTransparentTls_ = true;
   init();
   if (server) {
index fbbf999b3fc8a4b62af9f9b3fc0cd2f6dcec7b44..723599250a185cfe9ab497d1a1a8c42cc8d53034 100644 (file)
@@ -1959,6 +1959,43 @@ TEST(AsyncSSLSocketTest, TestPreReceivedData) {
       serverSock->getRawBytesReceived(), clientSock->getRawBytesWritten());
 }
 
+TEST(AsyncSSLSocketTest, TestMoveFromAsyncSocket) {
+  EventBase clientEventBase;
+  EventBase serverEventBase;
+  auto clientCtx = std::make_shared<SSLContext>();
+  auto dfServerCtx = std::make_shared<SSLContext>();
+  std::array<int, 2> fds;
+  getfds(fds.data());
+  getctx(clientCtx, dfServerCtx);
+
+  AsyncSSLSocket::UniquePtr clientSockPtr(
+      new AsyncSSLSocket(clientCtx, &clientEventBase, fds[0], false));
+  AsyncSocket::UniquePtr serverSockPtr(
+      new AsyncSocket(&serverEventBase, fds[1]));
+  auto clientSock = clientSockPtr.get();
+  auto serverSock = serverSockPtr.get();
+  SSLHandshakeClient client(std::move(clientSockPtr), true, true);
+
+  // Steal some data from the server.
+  clientEventBase.loopOnce();
+  std::array<uint8_t, 10> buf;
+  recv(fds[1], buf.data(), buf.size(), 0);
+  serverSock->setPreReceivedData(IOBuf::wrapBuffer(range(buf)));
+  AsyncSSLSocket::UniquePtr serverSSLSockPtr(
+      new AsyncSSLSocket(dfServerCtx, std::move(serverSockPtr), true));
+  auto serverSSLSock = serverSSLSockPtr.get();
+  SSLHandshakeServer server(std::move(serverSSLSockPtr), true, true);
+  while (!client.handshakeSuccess_ && !client.handshakeError_) {
+    serverEventBase.loopOnce();
+    clientEventBase.loopOnce();
+  }
+
+  EXPECT_TRUE(client.handshakeSuccess_);
+  EXPECT_TRUE(server.handshakeSuccess_);
+  EXPECT_EQ(
+      serverSSLSock->getRawBytesReceived(), clientSock->getRawBytesWritten());
+}
+
 /**
  * Test overriding the flags passed to "sendmsg()" system call,
  * and verifying that write requests fail properly.