]> Cypherpunks repositories - gostls13.git/commitdiff
database/sql: fail on unsupported options when context is un-cancellable
authorMatt Dee <mdee@hioscar.com>
Tue, 8 Aug 2017 19:58:27 +0000 (15:58 -0400)
committerDaniel Theophanes <kardianos@gmail.com>
Wed, 9 Aug 2017 20:06:20 +0000 (20:06 +0000)
Currently, the check for `ctx.Done() == context.Background().Done()`
comes before the check to see if we are ignoring any options.  That
check should be done earlier, so that the options are not silently
ignored.

Fixes #21350

Change-Id: I3704e4209854c7d99f3f92498bae831cabc7e419
Reviewed-on: https://go-review.googlesource.com/53970
Reviewed-by: Daniel Theophanes <kardianos@gmail.com>
Run-TryBot: Daniel Theophanes <kardianos@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/database/sql/ctxutil.go
src/database/sql/sql_test.go

index bd652b54625337b211fe1b9466239acfa104b190..b73ee86594257927622c5964ac4243482a707a85 100644 (file)
@@ -107,10 +107,6 @@ func ctxDriverBegin(ctx context.Context, opts *TxOptions, ci driver.Conn) (drive
                return ciCtx.BeginTx(ctx, dopts)
        }
 
-       if ctx.Done() == context.Background().Done() {
-               return ci.Begin()
-       }
-
        if opts != nil {
                // Check the transaction level. If the transaction level is non-default
                // then return an error here as the BeginTx driver value is not supported.
@@ -125,6 +121,10 @@ func ctxDriverBegin(ctx context.Context, opts *TxOptions, ci driver.Conn) (drive
                }
        }
 
+       if ctx.Done() == context.Background().Done() {
+               return ci.Begin()
+       }
+
        txi, err := ci.Begin()
        if err == nil {
                select {
index c935eb4348025756269926628187719ed7d8754d..644769442eb1bea980896dcffcaebb0b3eab48c5 100644 (file)
@@ -439,6 +439,20 @@ func TestTxContextWait(t *testing.T) {
        waitForFree(t, db, 5*time.Second, 0)
 }
 
+// TestUnsupportedOptions checks that the database fails when a driver that
+// doesn't implement ConnBeginTx is used with non-default options and an
+// un-cancellable context.
+func TestUnsupportedOptions(t *testing.T) {
+       db := newTestDB(t, "people")
+       defer closeDB(t, db)
+       _, err := db.BeginTx(context.Background(), &TxOptions{
+               Isolation: LevelSerializable, ReadOnly: true,
+       })
+       if err == nil {
+               t.Fatal("expected error when using unsupported options, got nil")
+       }
+}
+
 func TestMultiResultSetQuery(t *testing.T) {
        db := newTestDB(t, "people")
        defer closeDB(t, db)