]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/hex: fix Dumper not always closing on Close call
authorTim Cooper <tim.cooper@layeh.com>
Tue, 17 Apr 2018 19:55:42 +0000 (16:55 -0300)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 17 Apr 2018 20:14:55 +0000 (20:14 +0000)
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>

src/encoding/hex/hex.go
src/encoding/hex/hex_test.go

index edc53954a1c81545edf5399e8646d10c7190a5ad..4cb26b6673442ff7c1907a94e06133b3d3299735 100644 (file)
@@ -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] = ' '
index f2223166495377b02c90257f430206f39ac0d680..6ba054ef9a0dfab68a818883b755c0f0a572fceb 100644 (file)
@@ -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 {