]> Cypherpunks.ru repositories - gostls13.git/commitdiff
src/log/slog: disallow == on Values
authorJonathan Amsterdam <jba@google.com>
Mon, 27 Mar 2023 14:21:10 +0000 (10:21 -0400)
committerJonathan Amsterdam <jba@google.com>
Mon, 27 Mar 2023 17:28:00 +0000 (17:28 +0000)
Comparing two Values with == is sensitive to the internal
representation of Values, and may not correspond to
equality on the Go values they represent. For example,

    StringValue("X") != StringValue(strings.ToUpper("x"))

because Go ends up doing a pointer comparison on the data
stored in the Values.

So make Values non-comparable by adding a non-comparable field.

Updates #56345.

Change-Id: Ieedbf454e631cda10bc6fcf470b57d3f1d2182cc
Reviewed-on: https://go-review.googlesource.com/c/go/+/479516
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/log/slog/attr.go
src/log/slog/record.go
src/log/slog/value.go

index 29431cb7a7d79b1e28b51d051eee16f1b9b8a76b..2e9bc0e6ef7a82640eba3f17aba7dca4ea9a0cb2 100644 (file)
@@ -82,3 +82,7 @@ func (a Attr) Equal(b Attr) bool {
 func (a Attr) String() string {
        return fmt.Sprintf("%s=%s", a.Key, a.Value)
 }
+
+func (a Attr) isEmpty() bool {
+       return a.Key == "" && a.Value.num == 0 && a.Value.any == nil
+}
index 08fb850df9d00e9311868572668c990b802c0338..0ee2a27f0ea6cae59ba9a0c3c6b455cfe31fc81d 100644 (file)
@@ -106,7 +106,7 @@ func (r *Record) AddAttrs(attrs ...Attr) {
        // and seeing if the Attr there is non-zero.
        if cap(r.back) > len(r.back) {
                end := r.back[:len(r.back)+1][len(r.back)]
-               if end != (Attr{}) {
+               if !end.isEmpty() {
                        panic("copies of a slog.Record were both modified")
                }
        }
index b434eb368a4d22ed3512fd24f45c0e7a8a7b44fe..fcfc884dc3abde81997736ad94b2b7ae9d91faa7 100644 (file)
@@ -17,6 +17,7 @@ import (
 // it can represent most small values without an allocation.
 // The zero Value corresponds to nil.
 type Value struct {
+       _ [0]func() // disallow ==
        // num holds the value for Kinds Int64, Uint64, Float64, Bool and Duration,
        // the string length for KindString, and nanoseconds since the epoch for KindTime.
        num uint64
@@ -371,7 +372,7 @@ func (v Value) group() []Attr {
 
 //////////////// Other
 
-// Equal reports whether v and w have equal keys and values.
+// Equal reports whether v and w represent the same Go value.
 func (v Value) Equal(w Value) bool {
        k1 := v.Kind()
        k2 := w.Kind()