Commit d576837e authored by Levin Zimmermann's avatar Levin Zimmermann

proto: Simplify debugging by printing function name in case of overflow

parent d3cae883
......@@ -44,7 +44,7 @@ func (e *mstructDecodeError) Error() string {
// mdecodeErr is called to normilize error when msgp.ReadXXX returns err when decoding path.
func mdecodeErr(path string, err error) error {
if err == msgp.ErrShortBytes {
return ErrDecodeOverflow
return &ErrDecodeOverflow{path}
}
return &mdecodeError{path, err}
}
......
......@@ -228,3 +228,12 @@ func (addr Address) String() string {
return net.JoinHostPort(addr.Host, fmt.Sprintf("%d", addr.Port))
}
}
// ErrDecodeOverflow is the error returned by neoMsgDecode when decoding hits buffer overflow
type ErrDecodeOverflow struct {
function string
}
func (e *ErrDecodeOverflow) Error() string {
return fmt.Sprintf("decode '%s': buffer overflow", e.function)
}
......@@ -70,7 +70,6 @@ import (
"lab.nexedi.com/kirr/neo/go/internal/packed"
"encoding/binary"
"errors"
"math"
)
......@@ -168,9 +167,6 @@ func (e Encoding) MsgDecode(msg Msg, data []byte) (nread int, err error) {
}
// ErrDecodeOverflow is the error returned by neoMsgDecode when decoding hits buffer overflow
var ErrDecodeOverflow = errors.New("decode: buffer overflow")
// ---- messages ----
//neo:proto enum
......
......@@ -152,7 +152,8 @@ func testMsgMarshal(t *testing.T, enc Encoding, msg Msg, encoded string) {
// decode must detect buffer overflow
for l := len(encoded) - 1; l >= 0; l-- {
n, err = enc.MsgDecode(msg2, data[:l])
if !(n == 0 && err == ErrDecodeOverflow) {
errDecodeOverFlow := new(ErrDecodeOverFlow)
if !(n == 0 && errors.As(err, &errDecodeOverFlow)) {
t.Errorf("%c/%v: decode overflow not detected on [:%v]", enc, typ, l)
}
......
......@@ -911,7 +911,7 @@ func (d *decoderCommon) generatedCode() string {
// NOTE for >0 check actual X in StdSizes{X} does not particularly matter
if (&types.StdSizes{8, 8}).Sizeof(d.typ) > 0 || d.enc != 'N' {
code.emit("\noverflow:")
code.emit("return 0, ErrDecodeOverflow")
code.emit("return 0, &ErrDecodeOverflow{\"%s\"}", d.typeName)
}
code.emit("}\n")
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment