Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue 718] Fix nil pointer dereference in TopicNameWithoutPartitionPart #734

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions pulsar/consumer_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import (
"sync"
"testing"

"github.com/apache/pulsar-client-go/pulsar/internal"
"github.com/apache/pulsar-client-go/pulsar/internal/crypto"

"github.com/stretchr/testify/assert"

"github.com/apache/pulsar-client-go/pulsar/internal"
)

func TestSingleMessageIDNoAckTracker(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion pulsar/internal/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@ func NewMetricsProvider(metricsCardinality int, userDefinedLabels map[string]str

func (mp *Metrics) GetLeveledMetrics(t string) *LeveledMetrics {
labels := make(map[string]string, 3)
tn, _ := ParseTopicName(t)
tn, err := ParseTopicName(t)
if err != nil {
return nil
}
topic := TopicNameWithoutPartitionPart(tn)
switch mp.metricsLevel {
case 4:
Expand Down
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 ""
}
hantmac marked this conversation as resolved.
Show resolved Hide resolved
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