Skip to content

Commit f36fb2d

Browse files
committed
4
Change-Id: I4830410ed2ea9e74550eaa5e5722e3b9aeb80d16
1 parent 323acb1 commit f36fb2d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/log/slog/multi_handler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
// Its Enable method reports whether any of the handlers' Enabled methods return true.
1414
// Its Handle, WithAttr and WithGroup methods call the corresponding method on each of the enabled handlers.
1515
func MultiHandler(handlers ...Handler) Handler {
16-
return &multiHandler{multi: handlers}
16+
h := make([]Handler, len(handlers))
17+
copy(h, handlers)
18+
return &multiHandler{multi: h}
1719
}
1820

1921
type multiHandler struct {

src/log/slog/multi_handler_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,18 @@ func TestMultiHandler(t *testing.T) {
122122
}
123123
})
124124
}
125+
126+
// Test that MultiHandler copies the input slice and is insulated from future modification.
127+
func TestMultiHandlerCopy(t *testing.T) {
128+
var buf1 bytes.Buffer
129+
h1 := NewTextHandler(&buf1, nil)
130+
slice := []Handler{h1}
131+
multi := MultiHandler(slice...)
132+
slice[0] = nil
133+
134+
err := multi.Handle(context.Background(), NewRecord(time.Now(), LevelInfo, "test message", 0))
135+
if err != nil {
136+
t.Errorf("Expected nil error, but got: %v", err)
137+
}
138+
checkLogOutput(t, buf1.String(), "time="+textTimeRE+` level=INFO msg="test message"`)
139+
}

0 commit comments

Comments
 (0)