Repository: nats-io/nats.go
Pull request #808 (2021-09-01 00:41:58)

[FIXED] Inboxes suffix would contain many zeros
This was introduced by PR https://github.com/nats-io/nats.go/pull/767
when the nuidSize was used instead of replySuffixLen to just build
the reply suffix. This meant that the suffix part with 22 characters
long and had lots of zeros at the end.

Added a test to make sure we don't break that in the future.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>


Patch:
diff --git a/README.md b/README.md
index ecac808..e17a166 100644
--- a/README.md
+++ b/README.md
@@ -3,8 +3,7 @@ A [Go](http://golang.org) client for the [NATS messaging system](https://nats.io
 
 [![License Apache 2](https://img.shields.io/badge/License-Apache2-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
 [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fnats-io%2Fgo-nats.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fnats-io%2Fgo-nats?ref=badge_shield)
-[![Go Report Card](https://goreportcard.com/badge/github.com/nats-io/nats.go)](https://goreportcard.com/report/github.com/nats-io/nats.go) [![Build Status](https://travis-ci.com/nats-io/nats.go.svg?branch=master)](http://travis-ci.com/nats-io/nats.go) [![GoDoc](https://img.shields.io/badge/GoDoc-reference-007d9c)](https://pkg.go.dev/github.com/nats-io/nats.go)
- [![Coverage Status](https://coveralls.io/repos/nats-io/nats.go/badge.svg?branch=master)](https://coveralls.io/r/nats-io/nats.go?branch=master)
+[![Go Report Card](https://goreportcard.com/badge/github.com/nats-io/nats.go)](https://goreportcard.com/report/github.com/nats-io/nats.go) [![Build Status](https://travis-ci.com/nats-io/nats.go.svg?branch=main)](https://travis-ci.com/nats-io/nats.go) [![GoDoc](https://img.shields.io/badge/GoDoc-reference-007d9c)](https://pkg.go.dev/github.com/nats-io/nats.go) [![Coverage Status](https://coveralls.io/repos/nats-io/nats.go/badge.svg?branch=main)](https://coveralls.io/r/nats-io/nats.go?branch=main)
 
 ## Installation
 
diff --git a/nats.go b/nats.go
index f0148e2..14b795d 100644
--- a/nats.go
+++ b/nats.go
@@ -3566,6 +3566,7 @@ func (nc *Conn) oldRequest(subj string, hdr, data []byte, timeout time.Duration)
 const (
 	InboxPrefix    = "_INBOX."
 	inboxPrefixLen = len(InboxPrefix)
+	replySuffixLen = 8 // Gives us 62^8
 	rdigits        = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 	base           = 62
 )
@@ -3615,7 +3616,7 @@ func (nc *Conn) newRespInbox() string {
 	sb.WriteString(nc.respSubPrefix)
 
 	rn := nc.respRand.Int63()
-	for i := 0; i < nuidSize; i++ {
+	for i := 0; i < replySuffixLen; i++ {
 		sb.WriteByte(rdigits[rn%base])
 		rn /= base
 	}


Test patch:
diff --git a/go_test.mod b/go_test.mod
index 65ac99c..78cb139 100644
--- a/go_test.mod
+++ b/go_test.mod
@@ -4,7 +4,7 @@ go 1.16
 
 require (
 	github.com/golang/protobuf v1.4.2
-	github.com/nats-io/nats-server/v2 v2.3.5-0.20210825221009-41a253dabb43
+	github.com/nats-io/nats-server/v2 v2.4.1-0.20210831212757-2539bbb957ef
 	github.com/nats-io/nkeys v0.3.0
 	github.com/nats-io/nuid v1.0.1
 	google.golang.org/protobuf v1.23.0
diff --git a/go_test.sum b/go_test.sum
index 050ae22..b6fda20 100644
--- a/go_test.sum
+++ b/go_test.sum
@@ -19,9 +19,9 @@ github.com/nats-io/jwt v1.2.2 h1:w3GMTO969dFg+UOKTmmyuu7IGdusK+7Ytlt//OYH/uU=
 github.com/nats-io/jwt v1.2.2/go.mod h1:/xX356yQA6LuXI9xWW7mZNpxgF2mBmGecH+Fj34sP5Q=
 github.com/nats-io/jwt/v2 v2.0.3 h1:i/O6cmIsjpcQyWDYNcq2JyZ3/VTF8SJ4JWluI5OhpvI=
 github.com/nats-io/jwt/v2 v2.0.3/go.mod h1:VRP+deawSXyhNjXmxPCHskrR6Mq50BqpEI5SEcNiGlY=
-github.com/nats-io/nats-server/v2 v2.3.5-0.20210825221009-41a253dabb43 h1:Sbb4QxNsccsPERg0C7uQX7/xgOCOTMIvDH9Ytb5MXsU=
-github.com/nats-io/nats-server/v2 v2.3.5-0.20210825221009-41a253dabb43/go.mod h1:jgHRB+EfZisUr6j50/g7Gcah7AR8qtk3as42DJmESCk=
-github.com/nats-io/nats.go v1.11.1-0.20210819195927-9053aa4200f0/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
+github.com/nats-io/nats-server/v2 v2.4.1-0.20210831212757-2539bbb957ef h1:fVi/sPmt1MjO1bA4M4jFlrCs9DYuZbpHPeKr7IQRM2Q=
+github.com/nats-io/nats-server/v2 v2.4.1-0.20210831212757-2539bbb957ef/go.mod h1:TUAhMFYh1VISyY/D4WKJUMuGHg8yHtoUTuxkbiej1lc=
+github.com/nats-io/nats.go v1.12.0/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
 github.com/nats-io/nkeys v0.2.0/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1tqEu/s=
 github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8=
 github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4=
diff --git a/nats_test.go b/nats_test.go
index 342c482..fd965b2 100644
--- a/nats_test.go
+++ b/nats_test.go
@@ -2702,3 +2702,37 @@ func TestCustomInboxPrefix(t *testing.T) {
 		t.Fatalf("did not receive ok: %q", resp.Data)
 	}
 }
+
+func TestRespInbox(t *testing.T) {
+	s := RunServerOnPort(-1)
+	defer s.Shutdown()
+
+	nc, err := Connect(s.ClientURL())
+	if err != nil {
+		t.Fatalf("Expected to connect to server, got %v", err)
+	}
+	defer nc.Close()
+
+	if _, err := nc.Subscribe("foo", func(msg *Msg) {
+		lastDot := strings.LastIndex(msg.Reply, ".")
+		if lastDot == -1 {
+			msg.Respond([]byte(fmt.Sprintf("Invalid reply subject: %q", msg.Reply)))
+			return
+		}
+		lastToken := msg.Reply[lastDot+1:]
+		if len(lastToken) != replySuffixLen {
+			msg.Respond([]byte(fmt.Sprintf("Invalid last token: %q", lastToken)))
+			return
+		}
+		msg.Respond(nil)
+	}); err != nil {
+		t.Fatalf("subscribe failed: %s", err)
+	}
+	resp, err := nc.Request("foo", []byte("check inbox"), time.Second)
+	if err != nil {
+		t.Fatalf("Request failed: %v", err)
+	}
+	if len(resp.Data) > 0 {
+		t.Fatalf("Error: %s", resp.Data)
+	}
+}