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

Empty uint64 array will unmarshal to nil if struct is not initialized #96

Closed
jtraglia opened this issue Jul 21, 2022 · 5 comments · Fixed by #97
Closed

Empty uint64 array will unmarshal to nil if struct is not initialized #96

jtraglia opened this issue Jul 21, 2022 · 5 comments · Fixed by #97

Comments

@jtraglia
Copy link

Hi there. Noticed something that I believe is a bug in sszgen.

Given a structure with a uint64 array field, the field will unmarshal to a nil value if (1) the array is empty and (2) the variable that calls UnmarshalSSZ is not initialized. And if you swap uint64 for byte it works as expected.

See the following program:

package main

import "fmt"

type Foo struct {
  Bar []uint64 `json:"bar" ssz-max:"1024"`
}

func main() {
  val := Foo{Bar:[]uint64{}}
  var dec Foo // not initialized

  enc, err := val.MarshalSSZ()
  if err != nil {
    panic(err)
  }
  err = dec.UnmarshalSSZ(enc)
  if err != nil {
    panic(err)
  }

  fmt.Println("val.Bar nil?", val.Bar == nil)
  fmt.Println("dec.Bar nil?", dec.Bar == nil)
}

When executed, this will output:

$ sszgen --path main.go && go run .
val.Bar nil? false
dec.Bar nil? true

On the other hand, if dec.Bar is initialized with anything (even an empty array), it will behave as expected:

func main() {
  val := Foo{Bar:[]uint64{}}
  dec := Foo{Bar:[]uint64{}} // initialized

  enc, err := val.MarshalSSZ()
  if err != nil {
    panic(err)
  }
  err = dec.UnmarshalSSZ(enc)
  if err != nil {
    panic(err)
  }

  fmt.Println("val.Bar nil?", val.Bar == nil)
  fmt.Println("dec.Bar nil?", dec.Bar == nil)
}

When executed, this will output:

$ sszgen --path main.go && go run .
val.Bar nil? false
dec.Bar nil? false

Fixes: flashbots/go-boost-utils#23

@ferranbt
Copy link
Owner

Can you share the generated go file?

@ferranbt
Copy link
Owner

I found the issue, it is not on the generated code but on this function that extends the uint64 array. It should be easy to fix.

@ferranbt
Copy link
Owner

Could you try the fix in #97 and see if it resolves the issue?

@jtraglia
Copy link
Author

Yes, but I won't be able to today. On a day trip away from my workstation. Thank you for the quick fix!

@jtraglia
Copy link
Author

@ferranbt Yes, it resolves the issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants