Skip to content

Commit

Permalink
Improve error message names
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Aug 7, 2024
1 parent c98805c commit 6d7d54c
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 43 deletions.
17 changes: 10 additions & 7 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func DecodeDynamicLength(buf []byte, maxSize int) (int, error) {
offset := binary.LittleEndian.Uint32(buf[:4])
length, ok := DivideInt(int(offset), bytesPerLengthOffset)
if !ok {
return 0, fmt.Errorf("bad")
return 0, fmt.Errorf("incorrect length division")
}
if length > maxSize {
return 0, fmt.Errorf("too big for the list")
Expand All @@ -255,12 +255,15 @@ func DecodeDynamicLength(buf []byte, maxSize int) (int, error) {
// UnmarshalDynamic unmarshals the dynamic items from the input
func UnmarshalDynamic(src []byte, length int, f func(indx int, b []byte) error) error {
var err error
size := uint64(len(src))

if length == 0 {
if size != 0 && size != 4 {
return ErrSize
}
return nil
}

size := uint64(len(src))

indx := 0
dst := src

Expand All @@ -277,10 +280,10 @@ func UnmarshalDynamic(src []byte, length int, f func(indx int, b []byte) error)
endOffset = uint64(len(src))
}
if offset > endOffset {
return fmt.Errorf("four")
return fmt.Errorf("incorrect end of offset: %d %d", offset, endOffset)
}
if endOffset > size {
return fmt.Errorf("five")
return fmt.Errorf("incorrect end of offset size: %d %d", endOffset, size)
}

err := f(indx, src[offset:endOffset])
Expand All @@ -302,10 +305,10 @@ func UnmarshalDynamic(src []byte, length int, f func(indx int, b []byte) error)
func DivideInt2(a, b, max int) (int, error) {
num, ok := DivideInt(a, b)
if !ok {
return 0, fmt.Errorf("xx")
return 0, fmt.Errorf("failed to divide int %d by %d", a, b)
}
if num > max {
return 0, fmt.Errorf("yy")
return 0, fmt.Errorf("num %d is greater than max %d", num, max)
}
return num, nil
}
Expand Down
37 changes: 37 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package ssz

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func TestBitlist(t *testing.T) {
Expand Down Expand Up @@ -106,3 +110,36 @@ func TestEncode_ExtendUint(t *testing.T) {
t.Fatal("uint8 cannot be nil")
}
}

func TestUnmarshalDynamic(t *testing.T) {
{
buf := []byte{}
buf = WriteOffset(buf, 4*1)
buf = append(buf, 0x1)

num, err := DecodeDynamicLength(buf, 10)
require.NoError(t, err)

err = UnmarshalDynamic(buf, num, func(i int, b []byte) error {
if !bytes.Equal(b, []byte{0x1}) {
return fmt.Errorf("bad")
}
return nil
})
require.NoError(t, err)
}

{
buf := []byte{}
buf = WriteOffset(buf, 0)
buf = append(buf, 0x1)

num, err := DecodeDynamicLength(buf, 10)
require.NoError(t, err)

err = UnmarshalDynamic(buf, num, func(i int, b []byte) error {
return nil
})
require.Equal(t, ErrSize, err)
}
}
48 changes: 24 additions & 24 deletions spectests/structs_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sszgen/generator/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (v *Value) umarshalContainer(start bool, dst string) (str string) {
return ssz.ErrOffset
}
{{ if .firstOffsetCheck }}
if {{.offset}} < {{.firstOffsetCheck}} {
if {{.offset}} != {{.firstOffsetCheck}} {
return ssz.ErrInvalidVariableOffset
}
{{ end }}
Expand Down
4 changes: 2 additions & 2 deletions sszgen/testcases/case1_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sszgen/testcases/case7_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sszgen/testcases/container_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sszgen/testcases/issue_127_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sszgen/testcases/issue_166_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6d7d54c

Please sign in to comment.