Skip to content

Panic saving a struct with nil value field and json serializer #7274

@mytlogos

Description

@mytlogos

GORM Playground Link

go-gorm/playground#770

Description

When i save a struct with a json serializer field and nil value, gorm panics with this stacktrace:

--- FAIL: TestGORM_failure (0.01s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x8c5295]

goroutine 53 [running]:
testing.tRunner.func1.2({0xf0dd80, 0x15d6180})
        /usr/local/go/src/testing/testing.go:1631 +0x3f7
testing.tRunner.func1()
        /usr/local/go/src/testing/testing.go:1634 +0x6b6
panic({0xf0dd80?, 0x15d6180?})
        /usr/local/go/src/runtime/panic.go:770 +0x132
gorm.io/gorm/schema.(*Field).setupValuerAndSetter.func17({0x1109750, 0x25cda20}, {0xf5ab00?, 0xc00034b280?, 0x0?}, {0xf15fa0, 0xc000350000})
        /media/MintData/clones/gorm-playground/gorm/schema/field.go:964 +0x1d5
gorm.io/gorm/callbacks.ConvertToAssignments.func2(0xc0000fd400, {0xf15fa0, 0xc000350000})
        /media/MintData/clones/gorm-playground/gorm/callbacks/update.go:149 +0x126
gorm.io/gorm/callbacks.ConvertToAssignments(0xc000450700)
        /media/MintData/clones/gorm-playground/gorm/callbacks/update.go:288 +0x187d
gorm.io/gorm/callbacks.RegisterDefaultCallbacks.Update.func7(0xc000296a80)
        /media/MintData/clones/gorm-playground/gorm/callbacks/update.go:74 +0x326
gorm.io/gorm.(*processor).Execute(0xc000348a00, 0xc000296a80)
        /media/MintData/clones/gorm-playground/gorm/callbacks.go:130 +0xa78
gorm.io/gorm.(*DB).Save(0xc00030dd70, {0xec8c00, 0xc00034b280})
        /media/MintData/clones/gorm-playground/gorm/finisher_api.go:106 +0x9b8
gorm.io/playground.TestGORM_failure(0xc00044eb60?)
        /media/MintData/clones/gorm-playground/main_test.go:29 +0x105
testing.tRunner(0xc00044eb60, 0x10163f0)
        /usr/local/go/src/testing/testing.go:1689 +0x21f
created by testing.(*T).Run in goroutine 1
        /usr/local/go/src/testing/testing.go:1742 +0x826
FAIL    gorm.io/playground      0.177s
FAIL

After debugging it seems that the schema.Field has the Serializer set with a jsonserializer.
But when it creates the serializer instance in

return &serializer{
it uses the field SerializeValuer instead of Serializer.
Then it will panic in
} else if err = s.Serializer.Scan(ctx, field, value, s.value); err == nil {
as it never set Serializer.
SerializeValuer is never used again in the codebase.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions