[C++] Fix dangling reference bug in getRandomName #8596
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
The current
getRandomName
function contains a simple dangling reference bug: when it attempts to return aconst char*
string value from the temporary string returned by thestd::stringstream::str
function. This sometimes "works" anyway in release mode depending on the platform it's running on, 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.Modifications
I found the existing method for generating random strings rather obscure, so I both fixed the bug and simplified the code. I made use of
boost::random
to provide a much more straightforward implementation. I use this pattern often for random names in my company's production projects, and it works well.Verifying this change
This code is called every time the client subscribes to a topic, so it gets plenty of coverage throughout the tests. I also manually verified that it's working correctly by printing the random names while I was testing. I've already included this fix in a local build of Pulsar I'm using for a new product at FactSet, and it's working well there.
The CI checks are failing with some error about a website deployment that seems unrelated to the C++ client. Is this a problem? Please let me know if I need to do anything special to get those checks to pass. Thanks!