tendermint / go-amino

Protobuf3 with Interface support - Designed for blockchains (deterministic, upgradeable, fast, and compact)
Other
259 stars 78 forks source link

pointers to false round-trip as nil #289

Closed msuess closed 4 years ago

msuess commented 5 years ago

Description

We're using go-amino in cosmos. We have pointers to boolean values in our message structs in order to be able to differentiate whether a field was omitted or set to false.

However, when a pointer to false is marshalled then unmarshalled (binary only, the JSON methods work), the resulting value is nil.

Test program

Source

package main

import (
    "fmt"
    "github.com/tendermint/go-amino"
)

func main() {
    var cdc = amino.NewCodec()

    type SimpleStruct struct {
        BoolPtrTrue  *bool
        BoolPtrFalse *bool
    }

    t := true
    f := false

    s := SimpleStruct{
        BoolPtrTrue:  &t,
        BoolPtrFalse: &f,
    }

    b, _ := cdc.MarshalBinaryBare(s)

    var s2 SimpleStruct
    cdc.UnmarshalBinaryBare(b, &s2)

    fmt.Println(s2.BoolPtrTrue)
    fmt.Println(*s2.BoolPtrTrue)
    fmt.Println(s2.BoolPtrFalse)
    fmt.Println(*s2.BoolPtrFalse)
}

Output

0x4141ca
true
<nil>
panic: runtime error: invalid memory address or nil pointer dereference

Playground link

https://play.golang.org/p/x05ph3bq2zd

Thank you!

tac0turtle commented 4 years ago

closed with #290