Skip to content

Commit

Permalink
[C++] Fix dangling reference bug in getRandomName (#8596)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
oversearch committed Nov 23, 2020
1 parent 72b9f5d commit 83f0345
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
3 changes: 3 additions & 0 deletions pulsar-client-cpp/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ lib*.so*
.pydevproject
.idea/
*.cbp
*.ninja*
.clangd/
compile_commands.json

# doxygen files
apidocs/
Expand Down
28 changes: 12 additions & 16 deletions pulsar-client-cpp/lib/ClientImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pulsar-client-cpp/lib/ClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 83f0345

Please sign in to comment.