Commit 55c04274ae for golang
commit 55c04274aeb5e84bc3d23ba52e06da500c8d6399
Author: Ahmed Mohamed <ahmed.ahmed32005@gmail.com>
Date: Sun Nov 22 21:07:35 2026 +0300
encoding/gob: parse ignored interface values instead of skipping them
The encoder splits an interface value around any types it defines, so
its byte count covers only the first piece. Skipping by that count lost
the definitions and resumed inside the value. Parse the value instead,
as decodeInterface does. That reaches compileIgnoreSingle, which now
returns its errors rather than leaving a nil engine in the cache.
Fixes #34879
Change-Id: Idd9a71131cd9a5849cdbd5fa19a3014111d240ac
Reviewed-on: https://go-review.googlesource.com/c/go/+/866743
Reviewed-by: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: Sean Liao <sean@liao.dev>
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
diff --git a/src/encoding/gob/decode.go b/src/encoding/gob/decode.go
index 555af8f19f..93eafe9465 100644
--- a/src/encoding/gob/decode.go
+++ b/src/encoding/gob/decode.go
@@ -714,8 +714,7 @@ func (dec *Decoder) decodeInterface(ityp reflect.Type, state *decoderState, valu
if concreteId < 0 {
error_(dec.err)
}
- // Byte count of value is next; we don't care what it is (it's there
- // in case we want to ignore the value by skipping it completely).
+ // Byte count of value is next; we don't care what it is.
state.decodeUint()
// Read the concrete value.
v := allocValue(typ)
@@ -748,12 +747,16 @@ func (dec *Decoder) ignoreInterface(state *decoderState) {
if id < 0 {
error_(dec.err)
}
- // At this point, the decoder buffer contains a delimited value. Just toss it.
- n, ok = state.getLength()
+ // The byte count is not the length of the value if the value defines
+ // types, so parse the value, which also records those types.
+ _, ok = state.getLength()
if !ok {
errorf("bad interface encoding: data length too large for buffer")
}
- state.b.Drop(n)
+ dec.decodeIgnoredValue(id)
+ if dec.err != nil {
+ error_(dec.err)
+ }
}
// decodeGobDecoder decodes something implementing the GobDecoder interface.
@@ -1124,14 +1127,15 @@ func (dec *Decoder) compileSingle(remoteId typeId, ut *userTypeInfo) (engine *de
}
// compileIgnoreSingle compiles the decoder engine for a non-struct top-level value that will be discarded.
-func (dec *Decoder) compileIgnoreSingle(remoteId typeId) *decEngine {
- engine := new(decEngine)
+func (dec *Decoder) compileIgnoreSingle(remoteId typeId) (engine *decEngine, err error) {
+ defer catchError(&err)
+ engine = new(decEngine)
engine.instr = make([]decInstr, 1) // one item
op := dec.decIgnoreOpFor(remoteId, make(map[typeId]*decOp))
ovfl := overflow(dec.typeString(remoteId))
engine.instr[0] = decInstr{*op, 0, nil, ovfl}
engine.numInstr = 1
- return engine
+ return
}
// compileDec compiles the decoder engine for a value. If the value is not a struct,
@@ -1222,7 +1226,7 @@ func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, er
if wire != nil && wire.StructT != nil {
*enginePtr, err = dec.compileDec(wireId, userType(emptyStructType))
} else {
- *enginePtr = dec.compileIgnoreSingle(wireId)
+ *enginePtr, err = dec.compileIgnoreSingle(wireId)
}
if err != nil {
delete(dec.ignorerCache, wireId)
diff --git a/src/encoding/gob/encoder_test.go b/src/encoding/gob/encoder_test.go
index ec2ab53884..24f430e750 100644
--- a/src/encoding/gob/encoder_test.go
+++ b/src/encoding/gob/encoder_test.go
@@ -684,6 +684,45 @@ func TestNestedInterfaces(t *testing.T) {
}
}
+// Issue 34879: ignoring an interface value must record the types defined
+// inside it, as decoding it would.
+func TestIgnoreInterfaceNestedTypes(t *testing.T) {
+ Register(new(Bug0Outer))
+ var buf bytes.Buffer
+ enc := NewEncoder(&buf)
+ if err := enc.Encode(Bug0Outer{Bug0Outer{[]float64{1}}}); err != nil {
+ t.Fatal(err)
+ }
+ if err := enc.Encode([]float64{2}); err != nil {
+ t.Fatal(err)
+ }
+ dec := NewDecoder(&buf)
+ if err := dec.Decode(nil); err != nil {
+ t.Fatal("ignoring:", err)
+ }
+ var f []float64
+ if err := dec.Decode(&f); err != nil {
+ t.Fatal(err)
+ }
+ if !slices.Equal(f, []float64{2}) {
+ t.Fatalf("got %v; want [2]", f)
+ }
+}
+
+// A failed compilation of an ignore engine must not leave a broken engine
+// in the cache for the next value to use.
+func TestIgnoreUndefinedType(t *testing.T) {
+ // Two messages, each a value of the undefined type id 100.
+ data := []byte{0x03, 0xff, 0xc8, 0x00, 0x03, 0xff, 0xc8, 0x00}
+ dec := NewDecoder(bytes.NewReader(data))
+ for i := range 2 {
+ err := dec.Decode(nil)
+ if err == nil || !strings.Contains(err.Error(), "undefined type") {
+ t.Fatalf("#%d: got error %v; want undefined type", i, err)
+ }
+ }
+}
+
// The bugs keep coming. We forgot to send map subtypes before the map.
type Bug1Elem struct {