Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth: bugfix. ensure Bytes are reset during unmarshal #179

Merged
merged 1 commit into from
Oct 12, 2023
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
1 change: 1 addition & 0 deletions eth/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (hb *Bytes) UnmarshalJSON(data []byte) error {
n := len(data)/2 - len(*hb)
*hb = append(*hb, make(Bytes, n)...)
}
*hb = (*hb)[:len(data)/2]
_, err := hex.Decode(*hb, data)
return err
}
Expand Down
8 changes: 8 additions & 0 deletions eth/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ func TestBytes(t *testing.T) {
}
}

func TestBytes_Reuse(t *testing.T) {
x := struct{ D Bytes }{}
json.Unmarshal([]byte(`{"D": "0xdeadbeef"}`), &x)
diff.Test(t, t.Errorf, h2b("deadbeef"), x.D.Bytes())
json.Unmarshal([]byte(`{"D": "0xde"}`), &x)
diff.Test(t, t.Errorf, h2b("de"), x.D.Bytes())
}

func TestBytes_Write(t *testing.T) {
var x Bytes
diff.Test(t, t.Errorf, 0, len(x))
Expand Down