From 8dc8431ef9bb801d9387d1c2ddbb207f47820058 Mon Sep 17 00:00:00 2001 From: malstoun Date: Fri, 3 Jan 2020 19:43:19 +0300 Subject: [PATCH] Add arguments count check to execParamsAndPreparedPrefix --- conn.go | 4 ++++ conn_test.go | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/conn.go b/conn.go index 58195f1f7..014e1e2dc 100644 --- a/conn.go +++ b/conn.go @@ -501,6 +501,10 @@ func (c *Conn) execSimpleProtocol(ctx context.Context, sql string, arguments []i } func (c *Conn) execParamsAndPreparedPrefix(sd *pgconn.StatementDescription, arguments []interface{}) error { + if len(sd.ParamOIDs) != len(arguments) { + return errors.Errorf("expected %d arguments, got %d", len(sd.ParamOIDs), len(arguments)) + } + c.eqb.Reset() args, err := convertDriverValuers(arguments) diff --git a/conn_test.go b/conn_test.go index b8b5f4606..e5e607b05 100644 --- a/conn_test.go +++ b/conn_test.go @@ -200,6 +200,12 @@ func TestExecFailureWithArguments(t *testing.T) { t.Fatal("Expected SQL syntax error") } assert.False(t, pgconn.SafeToRetry(err)) + + _, err = conn.Exec(context.Background(), "select $1::varchar(1);", "1", "2") + if err == nil { + t.Fatal("Expected pgx arguments count error", err) + } + assert.Equal(t, "expected 1 arguments, got 2", err.Error()) } func TestExecContextWithoutCancelation(t *testing.T) {