You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2020/02/13 21:42:30 UTC

[thrift] branch master updated: THRIFT-5092: Return an error instead of causing a panic when attempting to write to a closed client. Client: go Patch: Renan I. Del Valle

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

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git


The following commit(s) were added to refs/heads/master by this push:
     new aa9e7e8  THRIFT-5092: Return an error instead of causing a panic when attempting to write to a closed client. Client: go Patch: Renan I. Del Valle
aa9e7e8 is described below

commit aa9e7e8f5966ec9233ef418bab81b48c5a539028
Author: Renan I. Del Valle <co...@ridv.xyz>
AuthorDate: Thu Feb 13 11:20:04 2020 -0800

    THRIFT-5092: Return an error instead of causing a panic when attempting to write to a closed client.
    Client: go
    Patch: Renan I. Del Valle
    
    This closes #2005
---
 lib/go/thrift/http_client.go      | 15 +++++++++++++--
 lib/go/thrift/http_client_test.go |  7 +++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/lib/go/thrift/http_client.go b/lib/go/thrift/http_client.go
index d093eeb..1924a1a 100644
--- a/lib/go/thrift/http_client.go
+++ b/lib/go/thrift/http_client.go
@@ -166,19 +166,30 @@ func (p *THttpClient) Read(buf []byte) (int, error) {
 }
 
 func (p *THttpClient) ReadByte() (c byte, err error) {
+	if p.response == nil {
+		return 0, NewTTransportException(NOT_OPEN, "Response buffer is empty, no request.")
+	}
 	return readByte(p.response.Body)
 }
 
 func (p *THttpClient) Write(buf []byte) (int, error) {
-	n, err := p.requestBuffer.Write(buf)
-	return n, err
+	if p.requestBuffer == nil {
+		return 0, NewTTransportException(NOT_OPEN, "Request buffer is nil, connection may have been closed.")
+	}
+	return p.requestBuffer.Write(buf)
 }
 
 func (p *THttpClient) WriteByte(c byte) error {
+	if p.requestBuffer == nil {
+		return NewTTransportException(NOT_OPEN, "Request buffer is nil, connection may have been closed.")
+	}
 	return p.requestBuffer.WriteByte(c)
 }
 
 func (p *THttpClient) WriteString(s string) (n int, err error) {
+	if p.requestBuffer == nil {
+		return 0, NewTTransportException(NOT_OPEN, "Request buffer is nil, connection may have been closed.")
+	}
 	return p.requestBuffer.WriteString(s)
 }
 
diff --git a/lib/go/thrift/http_client_test.go b/lib/go/thrift/http_client_test.go
index 453680a..a7977a3 100644
--- a/lib/go/thrift/http_client_test.go
+++ b/lib/go/thrift/http_client_test.go
@@ -35,6 +35,13 @@ func TestHttpClient(t *testing.T) {
 		t.Fatalf("Unable to connect to %s: %s", addr.String(), err)
 	}
 	TransportTest(t, trans, trans)
+
+	t.Run("nilBuffer", func(t *testing.T) {
+		_ = trans.Close()
+		if _, err = trans.Write([]byte{1, 2, 3, 4}); err == nil {
+			t.Fatalf("writing to a closed transport did not result in an error")
+		}
+	})
 }
 
 func TestHttpClientHeaders(t *testing.T) {