log: clone terminal handler attrs

Clone the existing terminal handler attrs before appending new attrs. This avoids a potential attr memory overwrite when append reuses the backing array shared with the parent handler.
This commit is contained in:
Weixie Cui 2026-06-11 00:32:12 +08:00
parent 08aaa7c5ff
commit ce893cb6db

View file

@ -7,6 +7,7 @@ import (
"log/slog" "log/slog"
"math/big" "math/big"
"reflect" "reflect"
"slices"
"sync" "sync"
"time" "time"
@ -95,7 +96,7 @@ func (h *TerminalHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
wr: h.wr, wr: h.wr,
lvl: h.lvl, lvl: h.lvl,
useColor: h.useColor, useColor: h.useColor,
attrs: append(h.attrs, attrs...), attrs: append(slices.Clone(h.attrs), attrs...),
fieldPadding: make(map[string]int), fieldPadding: make(map[string]int),
} }
} }