aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/hex
diff options
context:
space:
mode:
authorTim Cooper <tim.cooper@layeh.com>2018-04-17 16:55:42 -0300
committerBrad Fitzpatrick <bradfitz@golang.org>2018-04-17 20:14:55 +0000
commit9db1dd074df62d18f6902f06c93c72da0a3ffd16 (patch)
tree68ac8e53d7ef7cbfcd81d24731dd72273d51d4e4 /src/encoding/hex
parentf83e4212688c8dfc9a34f7735fa74d14e7995388 (diff)
downloadgo-9db1dd074df62d18f6902f06c93c72da0a3ffd16.tar.gz
go-9db1dd074df62d18f6902f06c93c72da0a3ffd16.zip
encoding/hex: fix Dumper not always closing on Close call
Updates #23574 Change-Id: I1b87390679e0817a2f6e4e5938994ea32df87bd7 Reviewed-on: https://go-review.googlesource.com/107596 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding/hex')
-rw-r--r--src/encoding/hex/hex.go5
-rw-r--r--src/encoding/hex/hex_test.go13
2 files changed, 17 insertions, 1 deletions
diff --git a/src/encoding/hex/hex.go b/src/encoding/hex/hex.go
index edc53954a1..4cb26b6673 100644
--- a/src/encoding/hex/hex.go
+++ b/src/encoding/hex/hex.go
@@ -282,10 +282,13 @@ func (h *dumper) Write(data []byte) (n int, err error) {
func (h *dumper) Close() (err error) {
// See the comments in Write() for the details of this format.
- if h.used == 0 || h.closed {
+ if h.closed {
return
}
h.closed = true
+ if h.used == 0 {
+ return
+ }
h.buf[0] = ' '
h.buf[1] = ' '
h.buf[2] = ' '
diff --git a/src/encoding/hex/hex_test.go b/src/encoding/hex/hex_test.go
index f222316649..6ba054ef9a 100644
--- a/src/encoding/hex/hex_test.go
+++ b/src/encoding/hex/hex_test.go
@@ -204,6 +204,19 @@ func TestDumper_doubleclose(t *testing.T) {
}
}
+func TestDumper_earlyclose(t *testing.T) {
+ var out bytes.Buffer
+ dumper := Dumper(&out)
+
+ dumper.Close()
+ dumper.Write([]byte(`gopher`))
+
+ expected := ""
+ if out.String() != expected {
+ t.Fatalf("got:\n%#v\nwant:\n%#v", out.String(), expected)
+ }
+}
+
func TestDump(t *testing.T) {
var in [40]byte
for i := range in {