From fe1fc4ce66d4822cdfbc32ca7cea3ca779d78734 Mon Sep 17 00:00:00 2001 From: hantmac Date: Tue, 22 Feb 2022 10:15:52 +0800 Subject: [PATCH] fix nil pointer dereference in TopicNameWithoutPartitionPart Signed-off-by: hantmac --- pulsar/internal/topic_name.go | 3 +++ pulsar/internal/topic_name_test.go | 12 ++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pulsar/internal/topic_name.go b/pulsar/internal/topic_name.go index 86a1ebe246..481e41f5bb 100644 --- a/pulsar/internal/topic_name.go +++ b/pulsar/internal/topic_name.go @@ -107,6 +107,9 @@ func ParseTopicName(topic string) (*TopicName, error) { } func TopicNameWithoutPartitionPart(tn *TopicName) string { + if tn == nil { + return "" + } if tn.Partition < 0 { return tn.Name } diff --git a/pulsar/internal/topic_name_test.go b/pulsar/internal/topic_name_test.go index f08fcd0ef8..ab6537b3d7 100644 --- a/pulsar/internal/topic_name_test.go +++ b/pulsar/internal/topic_name_test.go @@ -104,20 +104,24 @@ func TestParseTopicNameErrors(t *testing.T) { func TestTopicNameWithoutPartitionPart(t *testing.T) { tests := []struct { - tn TopicName + tn *TopicName expected string }{ { - tn: TopicName{Name: "persistent://public/default/my-topic", Partition: -1}, + tn: &TopicName{Name: "persistent://public/default/my-topic", Partition: -1}, expected: "persistent://public/default/my-topic", }, { - tn: TopicName{Name: "persistent://public/default/my-topic-partition-0", Partition: 0}, + tn: &TopicName{Name: "persistent://public/default/my-topic-partition-0", Partition: 0}, expected: "persistent://public/default/my-topic", }, + { + tn: nil, + expected: "", + }, } for _, test := range tests { - assert.Equal(t, test.expected, TopicNameWithoutPartitionPart(&test.tn)) + assert.Equal(t, test.expected, TopicNameWithoutPartitionPart(test.tn)) } }