gomodule / redigo

Go client for Redis
Apache License 2.0
9.76k stars 1.25k forks source link

AddFlat on struct with null embedded pointer panics #621

Closed lwaddicor closed 2 years ago

lwaddicor commented 2 years ago

Hey,

When using AddFlat on a struct that contains an embedded pointer that is null, a panic occurs. This panic is a new behaviour that didn't occur in v1.8.1

https://goplay.tools/snippet/pa0iofoid_-

package main

import (
    "testing"

    "github.com/gomodule/redigo/redis"
)

func TestAnonymousEmbeddedStructToArgs(t *testing.T) {
    type Inner struct {
        Name string
        ID   int
    }

    type Outer struct {
        LocationID int
        *Inner
    }

    v := &Outer{
        LocationID: 1,
        // Uncomment me to prevent panic
        // Inner:      &Inner{},
    }

    args := redis.Args{}.AddFlat(v)
    _ = args
}
=== RUN   TestAnonymousEmbeddedStructToArgs
--- FAIL: TestAnonymousEmbeddedStructToArgs (0.00s)
[T+0000ms]
panic: reflect: indirection through nil pointer to embedded struct [recovered]
    panic: reflect: indirection through nil pointer to embedded struct

goroutine 6 [running]:
testing.tRunner.func1.2({0x5152e0, 0x560650})
    /usr/local/go-faketime/src/testing/testing.go:1389 +0x24e
testing.tRunner.func1()
    /usr/local/go-faketime/src/testing/testing.go:1392 +0x39f
panic({0x5152e0, 0x560650})
    /usr/local/go-faketime/src/runtime/panic.go:838 +0x207
reflect.Value.FieldByIndex({0x51fdc0?, 0xc000052900?, 0xc0000183b0?}, {0xc000018390, 0x2, 0x40c8c5?})
    /usr/local/go-faketime/src/reflect/value.go:1260 +0x1e5
github.com/gomodule/redigo/redis.flattenStruct({0xc000070f60, 0x0, 0x0}, {0x51fdc0?, 0xc000052900?, 0x5ec1b8?})
    /tmp/gopath3688007872/pkg/mod/github.com/gomodule/redigo@v1.8.8/redis/scan.go:651 +0x118
github.com/gomodule/redigo/redis.Args.AddFlat({0xc00003e760?, 0x0, 0x0}, {0x511260, 0xc000052900?})
    /tmp/gopath3688007872/pkg/mod/github.com/gomodule/redigo@v1.8.8/redis/scan.go:637 +0x23f
main.TestAnonymousEmbeddedStructToArgs(0x0?)
    /tmp/sandbox2949396859/prog.go:24 +0x45
testing.tRunner(0xc0001244e0, 0x53f398)
    /usr/local/go-faketime/src/testing/testing.go:1439 +0x102
created by testing.(*T).Run
    /usr/local/go-faketime/src/testing/testing.go:1486 +0x35f
[T+0001ms]
Program exited.
stevenh commented 2 years ago

Fixed in v1.8.9