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

xyz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pulsar-client-cpp.git


The following commit(s) were added to refs/heads/main by this push:
     new 69b86aa  Fix crash caused by Message::getTopicName when the message is 
a producer message (#503)
69b86aa is described below

commit 69b86aa12334fd6af5e5ad8d1e1a9a5eab7d3f76
Author: Yunze Xu <[email protected]>
AuthorDate: Sun Sep 28 09:48:55 2025 +0800

    Fix crash caused by Message::getTopicName when the message is a producer 
message (#503)
---
 lib/MessageImpl.cc   | 5 ++++-
 tests/MessageTest.cc | 5 +++++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/lib/MessageImpl.cc b/lib/MessageImpl.cc
index 3974c4c..17239a8 100644
--- a/lib/MessageImpl.cc
+++ b/lib/MessageImpl.cc
@@ -88,7 +88,10 @@ void MessageImpl::setTopicName(const 
std::shared_ptr<std::string>& topicName) {
     messageId.setTopicName(topicName);
 }
 
-const std::string& MessageImpl::getTopicName() { return *topicName_; }
+const std::string& MessageImpl::getTopicName() {
+    static const std::string emptyString = "";
+    return topicName_ ? *topicName_ : emptyString;
+}
 
 int MessageImpl::getRedeliveryCount() { return redeliveryCount_; }
 
diff --git a/tests/MessageTest.cc b/tests/MessageTest.cc
index 189d40e..513ea8d 100644
--- a/tests/MessageTest.cc
+++ b/tests/MessageTest.cc
@@ -147,3 +147,8 @@ TEST(MessageTest, testMessageImplKeyValuePayloadCovert) {
         ASSERT_EQ(deMsgImpl.keyValuePtr->getValueAsString(), valueContent);
     }
 }
+
+TEST(MessageTest, testGetTopicNameOnProducerMessage) {
+    auto msg = MessageBuilder().setContent("test").build();
+    ASSERT_TRUE(msg.getTopicName().empty());
+}

Reply via email to