You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2020/11/23 19:03:06 UTC

[pulsar] branch master updated: [C++] Fix dangling reference bug in getRandomName (#8596)

This is an automated email from the ASF dual-hosted git repository.

mmerli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 83f0345  [C++] Fix dangling reference bug in getRandomName  (#8596)
83f0345 is described below

commit 83f0345dfe220fd82314f2a0648bd55c7561e5ea
Author: Brett <38...@users.noreply.github.com>
AuthorDate: Mon Nov 23 13:02:36 2020 -0600

    [C++] Fix dangling reference bug in getRandomName  (#8596)
    
    * The current getRandomName function contains a simple dangling reference bug: when it attempts to return a "const char*" string value from the temporary string returned by the stringstream "str" function.  This sometimes "works" anyway in release mode, but in debug mode on all the versions of GCC and CLANG I've tried it segfaults.  This makes debugging the c++ client very annoying.
    
    I also found the method for generating random strings rather obscure. So I both fixed the bug and simplified the code, making use of boost::random to provide a much more straightforward implementation.
    
    * Switched to std::random from boost::random, applied clang-format to the changed file.
---
 pulsar-client-cpp/.gitignore        |  3 +++
 pulsar-client-cpp/lib/ClientImpl.cc | 28 ++++++++++++----------------
 pulsar-client-cpp/lib/ClientImpl.h  |  2 +-
 3 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/pulsar-client-cpp/.gitignore b/pulsar-client-cpp/.gitignore
index 76efe8e..c446ac4 100644
--- a/pulsar-client-cpp/.gitignore
+++ b/pulsar-client-cpp/.gitignore
@@ -55,6 +55,9 @@ lib*.so*
 .pydevproject
 .idea/
 *.cbp
+*.ninja*
+.clangd/
+compile_commands.json
 
 # doxygen files
 apidocs/
diff --git a/pulsar-client-cpp/lib/ClientImpl.cc b/pulsar-client-cpp/lib/ClientImpl.cc
index b46ec35..401ddde 100644
--- a/pulsar-client-cpp/lib/ClientImpl.cc
+++ b/pulsar-client-cpp/lib/ClientImpl.cc
@@ -29,11 +29,11 @@
 #include "SimpleLoggerImpl.h"
 #include <boost/algorithm/string/predicate.hpp>
 #include <sstream>
-#include <openssl/sha.h>
 #include <lib/HTTPLookupService.h>
 #include <lib/TopicName.h>
 #include <algorithm>
 #include <regex>
+#include <random>
 #include <mutex>
 #ifdef USE_LOG4CXX
 #include "Log4CxxLogger.h"
@@ -45,24 +45,20 @@ namespace pulsar {
 
 static const char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                                  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+static std::uniform_int_distribution<> hexDigitsDist(0, sizeof(hexDigits));
+static std::mt19937 randomEngine =
+    std::mt19937(std::chrono::high_resolution_clock::now().time_since_epoch().count());
 
-const std::string generateRandomName() {
-    unsigned char hash[SHA_DIGEST_LENGTH];  // == 20;
-    boost::posix_time::ptime t(boost::posix_time::microsec_clock::universal_time());
-    long nanoSeconds = t.time_of_day().total_nanoseconds();
-    std::stringstream ss;
-    ss << nanoSeconds;
-    SHA1(reinterpret_cast<const unsigned char*>(ss.str().c_str()), ss.str().length(), hash);
-
-    const int nameLength = 10;
-    std::stringstream hexHash;
-    for (int i = 0; i < nameLength / 2; i++) {
-        hexHash << hexDigits[(hash[i] & 0xF0) >> 4];
-        hexHash << hexDigits[hash[i] & 0x0F];
-    }
+std::string generateRandomName() {
+    const int randomNameLength = 10;
 
-    return hexHash.str();
+    std::string randomName;
+    for (int i = 0; i < randomNameLength; ++i) {
+        randomName += hexDigits[hexDigitsDist(randomEngine)];
+    }
+    return randomName;
 }
+
 typedef std::unique_lock<std::mutex> Lock;
 
 typedef std::vector<std::string> StringList;
diff --git a/pulsar-client-cpp/lib/ClientImpl.h b/pulsar-client-cpp/lib/ClientImpl.h
index 46d713c..d1c5a3f 100644
--- a/pulsar-client-cpp/lib/ClientImpl.h
+++ b/pulsar-client-cpp/lib/ClientImpl.h
@@ -42,7 +42,7 @@ class ReaderImpl;
 typedef std::shared_ptr<ReaderImpl> ReaderImplPtr;
 typedef std::weak_ptr<ReaderImpl> ReaderImplWeakPtr;
 
-const std::string generateRandomName();
+std::string generateRandomName();
 
 class ClientImpl : public std::enable_shared_from_this<ClientImpl> {
    public: