Fix extra bytes in err message
authorSubodh Iyengar <subodh@fb.com>
Fri, 13 May 2016 07:24:05 +0000 (00:24 -0700)
committerFacebook Github Bot 8 <facebook-github-bot-8-bot@fb.com>
Fri, 13 May 2016 07:38:37 +0000 (00:38 -0700)
Summary:
Error message might be < 256 bytes
in which case buf.size() would be greater
than the size of the message written out
and we might have garbled bytes at the end.

ERR_error_string_n null terminates the string
so just use that.

Reviewed By: anirudhvr

Differential Revision: D3297320

fbshipit-source-id: 2ae626ce4c49ca478806c0bcc40a390e5a84f24b

folly/io/async/ssl/SSLErrors.cpp
folly/io/async/ssl/test/SSLErrorsTest.cpp [new file with mode: 0644]

index ebc5d3d0fa456ac3bac115739375310b6a0e1ebe..61987aed5e9df5e9c5c11a0feb7bcaab689ff164 100644 (file)
@@ -40,7 +40,8 @@ std::string decodeOpenSSLError(
   } else {
     std::array<char, 256> buf;
     ERR_error_string_n(errError, buf.data(), buf.size());
-    return std::string(buf.data(), buf.size());
+    // OpenSSL will null terminate the string.
+    return std::string(buf.data());
   }
 }
 
diff --git a/folly/io/async/ssl/test/SSLErrorsTest.cpp b/folly/io/async/ssl/test/SSLErrorsTest.cpp
new file mode 100644 (file)
index 0000000..f651ace
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2016 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/io/async/ssl/SSLErrors.h>
+
+#include <gtest/gtest.h>
+#include <openssl/err.h>
+#include <openssl/x509.h>
+
+using namespace testing;
+using namespace folly;
+
+TEST(SSLErrorsTest, TestMessage) {
+  ERR_load_crypto_strings();
+  auto err = ERR_PACK(
+      ERR_LIB_X509,
+      X509_F_X509_STORE_ADD_CERT,
+      X509_R_CERT_ALREADY_IN_HASH_TABLE);
+  SSLException ex(0, err, 0, 0);
+  std::string expectedMsg =
+      "AsyncSocketException: error:0B07C065:"
+      "x509 certificate routines:X509_STORE_add_cert:"
+      "cert already in hash table, type = SSL error";
+  std::string actual = ex.what();
+  EXPECT_EQ(expectedMsg, actual);
+}