Skip to content

Commit

Permalink
fix nil pointer dereference in TopicNameWithoutPartitionPart
Browse files Browse the repository at this point in the history
Signed-off-by: hantmac <[email protected]>
  • Loading branch information
hantmac committed Feb 22, 2022
1 parent 1df5596 commit fe1fc4c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pulsar/internal/topic_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 8 additions & 4 deletions pulsar/internal/topic_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down

0 comments on commit fe1fc4c

Please sign in to comment.