Skip to content

Commit

Permalink
fix: driver.Valuer returns itself causes stackoverflow
Browse files Browse the repository at this point in the history
Fixes: #657
  • Loading branch information
mcdoker18 committed Aug 19, 2022
1 parent 9bbbe7b commit c9f51d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
30 changes: 30 additions & 0 deletions internal/dbtest/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ func TestDB(t *testing.T) {
{testJSONMarshaler},
{testNilDriverValue},
{testRunInTxAndSavepoint},
{testEmbedTypeField},
{testDriverValuerReturnsItself},
}

testEachDB(t, func(t *testing.T, dbName string, db *bun.DB) {
Expand Down Expand Up @@ -1641,3 +1643,31 @@ func testRunInTxAndSavepoint(t *testing.T, db *bun.DB) {
require.NoError(t, err)
require.Equal(t, 4, count)
}

type anotherString string

var (
_ driver.Valuer = (*anotherString)(nil)
)

func (v anotherString) Value() (driver.Value, error) {
return v, nil
}

func testDriverValuerReturnsItself(t *testing.T, db *bun.DB) {
var expectedValue = anotherString("example value")

type Model struct {
ID int `bun:",pk,autoincrement"`
Value anotherString `bun:"value"`
}

ctx := context.Background()

err := db.ResetModel(ctx, (*Model)(nil))
require.NoError(t, err)

model := &Model{Value: expectedValue}
_, err = db.NewInsert().Model(model).Exec(ctx)
require.Error(t, err)
}
3 changes: 3 additions & 0 deletions schema/append_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ func appendDriverValue(fmter Formatter, b []byte, v reflect.Value) []byte {
if err != nil {
return dialect.AppendError(b, err)
}
if _, ok := value.(driver.Valuer); ok {
return dialect.AppendError(b, fmt.Errorf("driver.Valuer returns unsupported type %T", value))
}
return Append(fmter, b, value)
}

Expand Down

0 comments on commit c9f51d3

Please sign in to comment.