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

Pointer embedded struct handled incorrecly #369

Closed
ncsibra opened this issue Mar 10, 2022 · 0 comments
Closed

Pointer embedded struct handled incorrecly #369

ncsibra opened this issue Mar 10, 2022 · 0 comments

Comments

@ncsibra
Copy link

ncsibra commented Mar 10, 2022

Hi,

I tried to update from version v1.1.1 to v1.2.7, but I found a bug.
Pointer embedded structs are handled incorrectly, when the embedded struct is nil, after a encode/decode the nil struct gets a default value, instead of staying nil.

To reproduce, use this code:

go.mod:

module ugorji-test

go 1.17

require (
	github.com/davecgh/go-spew v1.1.1
	github.com/ugorji/go v1.1.1
//github.com/ugorji/go/codec v1.2.7
)

main.go

package main

import (
	"bytes"
	"fmt"
	"reflect"

	"github.com/davecgh/go-spew/spew"
	"github.com/ugorji/go/codec"
)

type Test struct {
	*Embedded
}

type Embedded struct {
	Field string
}

func main() {
	handle := &codec.MsgpackHandle{}

	orig := &Test{}
	buf := new(bytes.Buffer)

	enc := codec.NewEncoder(buf, handle)
	err := enc.Encode(orig)
	if err != nil {
		panic(err)
	}

	decoded := &Test{}

	dec := codec.NewDecoder(buf, handle)
	err = dec.Decode(decoded)
	if err != nil {
		panic(err)
	}

	if !reflect.DeepEqual(orig, decoded) {
		fmt.Printf("orig: \n%v\n", spew.Sdump(orig))
		fmt.Printf("decoded: \n%v\n", spew.Sdump(decoded))
	} else {
		fmt.Println("orig and decoded are the same")
	}
}

When using v1.1.1 the result will be the orig and decoded are the same message.
With v1.2.7:

orig: 
(*main.Test)(0xc000010030)({
 Embedded: (*main.Embedded)(<nil>)
})

decoded:
(*main.Test)(0xc000010038)({
 Embedded: (*main.Embedded)(0xc000012230)({
  Field: (string) ""
 })
})

@ugorji ugorji closed this as completed in 1000ed4 May 21, 2022
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

No branches or pull requests

1 participant