Fix memory leak/corruption 97/198997/3
authorDariusz Michaluk <d.michaluk@samsung.com>
Thu, 31 Jan 2019 13:41:09 +0000 (14:41 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 1 Mar 2019 12:26:35 +0000 (12:26 +0000)
Change-Id: I8f9bed07752fde26f629cca6931231dab5fd8980

src/manager/common/openssl-error-handler.cpp

index 40c3d2b..e8649c1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2017 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
 #include <sstream>
 #include <string>
 #include <utility>
+#include <vector>
 
 #define OPENSSL_SUCCESS 1
 
@@ -57,19 +58,19 @@ const char *ckm_debug_translate_error(int err)
 
 void errorDump()
 {
-       BIO *bio = BIO_new(BIO_s_mem());
-       ERR_print_errors(bio);
-       char *buf = NULL;
-       long len = BIO_get_mem_data(bio, &buf);
-       if(len < 0) {
-               LogError("Fail in BIO_get_mem_data()");
+       typedef std::unique_ptr<BIO, std::function<void(BIO *)>> BioUniquePtr;
+       BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
+       if (!bio.get())
                return;
+
+       ERR_print_errors(bio.get());
+
+       std::vector<char> message(1024);
+       int len = BIO_read(bio.get(), message.data(), message.size());
+       if (len > 0) {
+               message.resize(len);
+               LogError(std::string(message.begin(), message.end()));
        }
-       size_t length = static_cast<size_t>(len);
-       BIO_free(bio);
-       std::string ret(buf, length);
-       free(buf);
-       LogError(ret);
 }
 
 void errorHandle(const char *file, int line, const char *function, int openssl_ret)