Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions schema/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ func scanJSON(dest reflect.Value, src any) error {
if src == nil {
return scanNull(dest)
}
if !dest.CanAddr() {
return fmt.Errorf("bun: Scan(nonaddressable %s)", dest.Type())
}

b, err := toBytes(src)
if err != nil {
Expand All @@ -365,6 +368,9 @@ func scanJSONUseNumber(dest reflect.Value, src any) error {
if src == nil {
return scanNull(dest)
}
if !dest.CanAddr() {
return fmt.Errorf("bun: Scan(nonaddressable %s)", dest.Type())
}

b, err := toBytes(src)
if err != nil {
Expand Down Expand Up @@ -531,6 +537,9 @@ func scanJSONIntoInterface(dest reflect.Value, src any) error {
}

dest = dest.Elem()
if !dest.CanAddr() {
return fmt.Errorf("bun: Scan(nonaddressable %s)", dest.Type())
}
if fn := Scanner(dest.Type()); fn != nil {
return fn(dest, src)
}
Expand Down
36 changes: 36 additions & 0 deletions schema/scan_json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package schema

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

// Regression test for #1306: scanning JSON into a non-nil interface used to
// panic on dest.Addr() because the interface element is not addressable. The
// scan must fail with an error instead, so the query does not deadlock.
func TestScanJSONUnaddressableInterface(t *testing.T) {
var value any = map[string]any{"a": float64(1)}
dest := reflect.ValueOf(&value).Elem()

fn := Scanner(dest.Type())
require.NotNil(t, fn)

require.NotPanics(t, func() {
err := fn(dest, []byte(`{"b":2}`))
require.Error(t, err)
require.Contains(t, err.Error(), "nonaddressable")
})
}

func TestScanJSONAddressableMap(t *testing.T) {
var value map[string]any
dest := reflect.ValueOf(&value).Elem()

fn := Scanner(dest.Type())
require.NotNil(t, fn)

require.NoError(t, fn(dest, []byte(`{"b":2}`)))
require.Equal(t, map[string]any{"b": float64(2)}, value)
}
Loading