Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions network/vpack/vpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,18 @@ func (d *StatelessDecoder) DecompressVote(dst, src []byte) ([]byte, error) {
return d.dst, nil
}

// stripMsgpFieldMarker removes the msgpack fixstr marker byte from a field string
// for cleaner error messages. msgpack field strings like "\xa3per" become "per".
func stripMsgpFieldMarker(fieldStr string) string {
if len(fieldStr) > 1 && (fieldStr[0]&0xe0) == 0xa0 {
return fieldStr[1:]
}
return fieldStr
}

func (d *StatelessDecoder) bin64(fieldStr string) error {
if d.pos+64 > len(d.src) {
return fmt.Errorf("not enough data to read value for field %s", fieldStr)
return fmt.Errorf("not enough data to read value for field %s", stripMsgpFieldMarker(fieldStr))
}
d.dst = append(d.dst, fieldStr...)
d.dst = append(d.dst, msgpBin8Len64...)
Expand All @@ -351,7 +360,7 @@ func (d *StatelessDecoder) bin64(fieldStr string) error {

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

func (d *StatelessDecoder) bin80(fieldStr string) error {
if d.pos+80 > len(d.src) {
return fmt.Errorf("not enough data to read value for field %s, d.pos=%d, len(src)=%d", fieldStr, d.pos, len(d.src))
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))
}
d.dst = append(d.dst, fieldStr...)
d.dst = append(d.dst, msgpBin8Len80...)
Expand All @@ -373,16 +382,16 @@ func (d *StatelessDecoder) bin80(fieldStr string) error {

func (d *StatelessDecoder) varuint(fieldName string) error {
if d.pos+1 > len(d.src) {
return fmt.Errorf("not enough data to read varuint marker for field %s", fieldName)
return fmt.Errorf("not enough data to read varuint marker for field %s", stripMsgpFieldMarker(fieldName))
}
marker := d.src[d.pos] // read msgpack varuint marker
moreBytes, err := msgpVaruintRemaining(marker)
if err != nil {
return fmt.Errorf("invalid varuint marker %d for field %s: %w", marker, fieldName, err)
return fmt.Errorf("invalid varuint marker %d for field %s: %w", marker, stripMsgpFieldMarker(fieldName), err)
}

if d.pos+1+moreBytes > len(d.src) {
return fmt.Errorf("not enough data for varuint (need %d bytes) for field %s", moreBytes, fieldName)
return fmt.Errorf("not enough data for varuint (need %d bytes) for field %s", moreBytes, stripMsgpFieldMarker(fieldName))
}
d.dst = append(d.dst, fieldName...)
d.dst = append(d.dst, marker)
Expand Down
31 changes: 15 additions & 16 deletions network/vpack/vpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package vpack

import (
"encoding/json"
"fmt"
"reflect"
"slices"
"testing"
Expand Down Expand Up @@ -123,45 +122,45 @@ func TestStatelessDecoderErrors(t *testing.T) {

cases := []tc{
// ---------- cred ----------
{"pf-bin80", fmt.Sprintf("field %s", msgpFixstrPf),
{"pf-bin80", "field pf",
Comment thread
cce marked this conversation as resolved.
func() []byte { return slices.Concat(h(0), z(79)) }},

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

// ---------- r.prop.* ----------
{"dig-bin32", fmt.Sprintf("field %s", msgpFixstrDig),
{"dig-bin32", "field dig",
func() []byte { return slices.Concat(h(bitDig), pf, z(10)) }},
{"encdig-bin32", fmt.Sprintf("field %s", msgpFixstrEncdig),
{"encdig-bin32", "field encdig",
func() []byte { return slices.Concat(h(bitDig|bitEncDig), pf, z32, z(10)) }},
{"oper-varuint-marker", fmt.Sprintf("field %s", msgpFixstrOper),
{"oper-varuint-marker", "field oper",
func() []byte { return slices.Concat(h(bitOper), pf) }},
{"oprop-bin32", fmt.Sprintf("field %s", msgpFixstrOprop),
{"oprop-bin32", "field oprop",
func() []byte { return slices.Concat(h(bitOprop), pf, z(5)) }},

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

// ---------- r.snd / r.step ----------
{"snd-bin32", fmt.Sprintf("field %s", msgpFixstrSnd),
{"snd-bin32", "field snd",
func() []byte { return slices.Concat(h(0), pf, fix1) }},
{"step-varuint-marker", fmt.Sprintf("field %s", msgpFixstrStep),
{"step-varuint-marker", "field step",
func() []byte { return slices.Concat(h(bitStep), pf, fix1, z32) }},

// ---------- sig.* ----------
{"p-bin32", fmt.Sprintf("field %s", msgpFixstrP),
{"p-bin32", "field p",
func() []byte { return slices.Concat(h(0), pf, fix1, z32) }},
{"p1s-bin64", fmt.Sprintf("field %s", msgpFixstrP1s),
{"p1s-bin64", "field p1s",
func() []byte { return slices.Concat(h(0), pf, fix1, z32, z32, z(12)) }},
{"p2-bin32", fmt.Sprintf("field %s", msgpFixstrP2),
{"p2-bin32", "field p2",
func() []byte { return slices.Concat(h(0), pf, fix1, z32, z32, z64) }},
{"p2s-bin64", fmt.Sprintf("field %s", msgpFixstrP2s),
{"p2s-bin64", "field p2s",
func() []byte { return slices.Concat(h(0), pf, fix1, z32, z32, z64, z32, z(3)) }},
{"s-bin64", fmt.Sprintf("field %s", msgpFixstrS),
{"s-bin64", "field s",
func() []byte { return slices.Concat(h(0), pf, fix1, z32, z32, z64, z32, z64) }},

// ---------- trailing data ----------
Expand Down