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

Panic with json serializer and nil value #770

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mytlogos
Copy link

Explain your user case and expected results

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 https://github.com/go-gorm/gorm/blob/deceebfab8c460cfee229233aded2821ac6b08eb/schema/field.go#L494 it uses the field SerializeValuer instead of Serializer.
Then it will panic in https://github.com/go-gorm/gorm/blob/deceebfab8c460cfee229233aded2821ac6b08eb/schema/field.go#L964 as it never set Serializer.
SerializeValuer is never used again in the codebase.

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

Successfully merging this pull request may close these issues.

1 participant