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

Attempting to compact a slice panics #7

Closed
b1naryth1ef opened this issue Nov 22, 2013 · 2 comments
Closed

Attempting to compact a slice panics #7

b1naryth1ef opened this issue Nov 22, 2013 · 2 comments

Comments

@b1naryth1ef
Copy link

Trying to run the following code:

package main

import "github.com/vmihailenco/msgpack"
import "fmt"
import "unsafe"
import "reflect"

type Test struct {
    A uint16
    B uint16
    C string
    D [4]string
    E [2]int64
}

func main() {
    test := Test{
        A: uint16(1),
        B: uint16(25565),
        C: "yolotest",
        D: [4]string{"a", "b", "c", "d"},
        E: [2]int64{int64(1348293849324), int64(4345345345)},
    }
    buf, err := msgpack.Marshal(test)
    if err != nil {
        fmt.Printf("%s\n", err)
    }
    fmt.Printf("%s\n", buf)
    fmt.Printf("%s\n", len(buf))
    fmt.Printf("%s\n", unsafe.Sizeof(test))

    newzy := Test{}
    msgpack.Unmarshal(buf, &newzy)
    fmt.Printf("%s\n", newzy)
    fmt.Printf("%s\n", unsafe.Sizeof(newzy))
}

I get the following error:

panic: reflect: call of reflect.Value.IsNil on array Value

goroutine 1 [running]:
reflect.Value.IsNil(0x46ca98, 0xf840000e18, 0x112, 0x4aa070, 0x46ca98, ...)
    /usr/lib/go/src/pkg/reflect/value.go:895 +0x130
github.com/vmihailenco/msgpack.(*Encoder).encodeSlice(0xf84002f220, 0x46ca98, 0xf840000e18, 0x112, 0x1, ...)

This happens both when trying to compact a struct, and a single slice like:

func main() {
    test := [4]string{"a", "b", "c", "d"}
    buf, err := msgpack.Marshal(test)
    fmt.Printf("err: %s\n", err)
    newzy := make([]string, 4)
    msgpack.Unmarshal(buf, &newzy)
    fmt.Printf("V: %s\n", newzy)
}

If one was to comment the invalid IsNil call, you get the following error:

panic: interface conversion: interface is [4]string, not []string

goroutine 1 [running]:
github.com/vmihailenco/msgpack.(*Encoder).encodeSlice(0xf84002f220, 0x46ca98, 0xf840000e18, 0x112, 0x1, ...)
@vmihailenco
Copy link
Owner

Fixed. You probably need to read this http://blog.golang.org/slices , because [4]string is an array, not a slice.

@b1naryth1ef
Copy link
Author

TIL. Thanks for the patch! 👍

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

2 participants