Skip to content

Commit a51dbb4

Browse files
authored
network: clean up stateless vpack decode error messages (#6494)
1 parent b210374 commit a51dbb4

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

network/vpack/vpack.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,18 @@ func (d *StatelessDecoder) DecompressVote(dst, src []byte) ([]byte, error) {
338338
return d.dst, nil
339339
}
340340

341+
// stripMsgpFieldMarker removes the msgpack fixstr marker byte from a field string
342+
// for cleaner error messages. msgpack field strings like "\xa3per" become "per".
343+
func stripMsgpFieldMarker(fieldStr string) string {
344+
if len(fieldStr) > 1 && (fieldStr[0]&0xe0) == 0xa0 {
345+
return fieldStr[1:]
346+
}
347+
return fieldStr
348+
}
349+
341350
func (d *StatelessDecoder) bin64(fieldStr string) error {
342351
if d.pos+64 > len(d.src) {
343-
return fmt.Errorf("not enough data to read value for field %s", fieldStr)
352+
return fmt.Errorf("not enough data to read value for field %s", stripMsgpFieldMarker(fieldStr))
344353
}
345354
d.dst = append(d.dst, fieldStr...)
346355
d.dst = append(d.dst, msgpBin8Len64...)
@@ -351,7 +360,7 @@ func (d *StatelessDecoder) bin64(fieldStr string) error {
351360

352361
func (d *StatelessDecoder) bin32(fieldStr string) error {
353362
if d.pos+32 > len(d.src) {
354-
return fmt.Errorf("not enough data to read value for field %s", fieldStr)
363+
return fmt.Errorf("not enough data to read value for field %s", stripMsgpFieldMarker(fieldStr))
355364
}
356365
d.dst = append(d.dst, fieldStr...)
357366
d.dst = append(d.dst, msgpBin8Len32...)
@@ -362,7 +371,7 @@ func (d *StatelessDecoder) bin32(fieldStr string) error {
362371

363372
func (d *StatelessDecoder) bin80(fieldStr string) error {
364373
if d.pos+80 > len(d.src) {
365-
return fmt.Errorf("not enough data to read value for field %s, d.pos=%d, len(src)=%d", fieldStr, d.pos, len(d.src))
374+
return fmt.Errorf("not enough data to read value for field %s, d.pos=%d, len(src)=%d", stripMsgpFieldMarker(fieldStr), d.pos, len(d.src))
366375
}
367376
d.dst = append(d.dst, fieldStr...)
368377
d.dst = append(d.dst, msgpBin8Len80...)
@@ -373,16 +382,16 @@ func (d *StatelessDecoder) bin80(fieldStr string) error {
373382

374383
func (d *StatelessDecoder) varuint(fieldName string) error {
375384
if d.pos+1 > len(d.src) {
376-
return fmt.Errorf("not enough data to read varuint marker for field %s", fieldName)
385+
return fmt.Errorf("not enough data to read varuint marker for field %s", stripMsgpFieldMarker(fieldName))
377386
}
378387
marker := d.src[d.pos] // read msgpack varuint marker
379388
moreBytes, err := msgpVaruintRemaining(marker)
380389
if err != nil {
381-
return fmt.Errorf("invalid varuint marker %d for field %s: %w", marker, fieldName, err)
390+
return fmt.Errorf("invalid varuint marker %d for field %s: %w", marker, stripMsgpFieldMarker(fieldName), err)
382391
}
383392

384393
if d.pos+1+moreBytes > len(d.src) {
385-
return fmt.Errorf("not enough data for varuint (need %d bytes) for field %s", moreBytes, fieldName)
394+
return fmt.Errorf("not enough data for varuint (need %d bytes) for field %s", moreBytes, stripMsgpFieldMarker(fieldName))
386395
}
387396
d.dst = append(d.dst, fieldName...)
388397
d.dst = append(d.dst, marker)

network/vpack/vpack_test.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package vpack
1818

1919
import (
2020
"encoding/json"
21-
"fmt"
2221
"reflect"
2322
"slices"
2423
"testing"
@@ -123,45 +122,45 @@ func TestStatelessDecoderErrors(t *testing.T) {
123122

124123
cases := []tc{
125124
// ---------- cred ----------
126-
{"pf-bin80", fmt.Sprintf("field %s", msgpFixstrPf),
125+
{"pf-bin80", "field pf",
127126
func() []byte { return slices.Concat(h(0), z(79)) }},
128127

129128
// ---------- r.per ----------
130-
{"per-varuint-marker", fmt.Sprintf("field %s", msgpFixstrPer),
129+
{"per-varuint-marker", "field per",
131130
func() []byte { return slices.Concat(h(bitPer), pf) }},
132131

133132
// ---------- r.prop.* ----------
134-
{"dig-bin32", fmt.Sprintf("field %s", msgpFixstrDig),
133+
{"dig-bin32", "field dig",
135134
func() []byte { return slices.Concat(h(bitDig), pf, z(10)) }},
136-
{"encdig-bin32", fmt.Sprintf("field %s", msgpFixstrEncdig),
135+
{"encdig-bin32", "field encdig",
137136
func() []byte { return slices.Concat(h(bitDig|bitEncDig), pf, z32, z(10)) }},
138-
{"oper-varuint-marker", fmt.Sprintf("field %s", msgpFixstrOper),
137+
{"oper-varuint-marker", "field oper",
139138
func() []byte { return slices.Concat(h(bitOper), pf) }},
140-
{"oprop-bin32", fmt.Sprintf("field %s", msgpFixstrOprop),
139+
{"oprop-bin32", "field oprop",
141140
func() []byte { return slices.Concat(h(bitOprop), pf, z(5)) }},
142141

143142
// ---------- r.rnd ----------
144-
{"rnd-varuint-marker", fmt.Sprintf("field %s", msgpFixstrRnd),
143+
{"rnd-varuint-marker", "field rnd",
145144
func() []byte { return slices.Concat(h(0), pf) }},
146-
{"rnd-varuint-trunc", fmt.Sprintf("not enough data for varuint (need 4 bytes) for field %s", msgpFixstrRnd),
145+
{"rnd-varuint-trunc", "not enough data for varuint (need 4 bytes) for field rnd",
147146
func() []byte { return slices.Concat(h(0), pf, []byte{msgpUint32, 0x00}) }},
148147

149148
// ---------- r.snd / r.step ----------
150-
{"snd-bin32", fmt.Sprintf("field %s", msgpFixstrSnd),
149+
{"snd-bin32", "field snd",
151150
func() []byte { return slices.Concat(h(0), pf, fix1) }},
152-
{"step-varuint-marker", fmt.Sprintf("field %s", msgpFixstrStep),
151+
{"step-varuint-marker", "field step",
153152
func() []byte { return slices.Concat(h(bitStep), pf, fix1, z32) }},
154153

155154
// ---------- sig.* ----------
156-
{"p-bin32", fmt.Sprintf("field %s", msgpFixstrP),
155+
{"p-bin32", "field p",
157156
func() []byte { return slices.Concat(h(0), pf, fix1, z32) }},
158-
{"p1s-bin64", fmt.Sprintf("field %s", msgpFixstrP1s),
157+
{"p1s-bin64", "field p1s",
159158
func() []byte { return slices.Concat(h(0), pf, fix1, z32, z32, z(12)) }},
160-
{"p2-bin32", fmt.Sprintf("field %s", msgpFixstrP2),
159+
{"p2-bin32", "field p2",
161160
func() []byte { return slices.Concat(h(0), pf, fix1, z32, z32, z64) }},
162-
{"p2s-bin64", fmt.Sprintf("field %s", msgpFixstrP2s),
161+
{"p2s-bin64", "field p2s",
163162
func() []byte { return slices.Concat(h(0), pf, fix1, z32, z32, z64, z32, z(3)) }},
164-
{"s-bin64", fmt.Sprintf("field %s", msgpFixstrS),
163+
{"s-bin64", "field s",
165164
func() []byte { return slices.Concat(h(0), pf, fix1, z32, z32, z64, z32, z64) }},
166165

167166
// ---------- trailing data ----------

0 commit comments

Comments
 (0)