You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by rx...@apache.org on 2022/05/24 04:10:04 UTC

[pulsar-client-go] branch master updated: fix nil pointer dereference in TopicNameWithoutPartitionPart (#734)

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

rxl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-client-go.git


The following commit(s) were added to refs/heads/master by this push:
     new a8270eb  fix nil pointer dereference in TopicNameWithoutPartitionPart (#734)
a8270eb is described below

commit a8270ebb7549e6206f82ffec0ea5c75d4f2f1ded
Author: Jeremy <ha...@outlook.com>
AuthorDate: Tue May 24 12:09:59 2022 +0800

    fix nil pointer dereference in TopicNameWithoutPartitionPart (#734)
    
    Signed-off-by: hantmac <ha...@outlook.com>
    
    add error check
    
    Signed-off-by: hantmac <ha...@outlook.com>
---
 pulsar/consumer_partition_test.go  |  4 +---
 pulsar/internal/metrics.go         |  5 ++++-
 pulsar/internal/topic_name.go      |  3 +++
 pulsar/internal/topic_name_test.go | 12 ++++++++----
 4 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/pulsar/consumer_partition_test.go b/pulsar/consumer_partition_test.go
index e7fcd5d..2255e52 100644
--- a/pulsar/consumer_partition_test.go
+++ b/pulsar/consumer_partition_test.go
@@ -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) {
diff --git a/pulsar/internal/metrics.go b/pulsar/internal/metrics.go
index ec1a96e..1cab470 100644
--- a/pulsar/internal/metrics.go
+++ b/pulsar/internal/metrics.go
@@ -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:
diff --git a/pulsar/internal/topic_name.go b/pulsar/internal/topic_name.go
index 86a1ebe..481e41f 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 f08fcd0..ab6537b 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))
 	}
 }