From d8d93a38df0048951ff15830d793024f890f6c3c Mon Sep 17 00:00:00 2001 From: Thanatat Tamtan Date: Wed, 26 Apr 2023 11:27:51 +0700 Subject: [PATCH] fix handle pgpass (#1120) --- conn.go | 17 ++++++++++------- conn_test.go | 4 ---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/conn.go b/conn.go index 94f659c0..a981bc0a 100644 --- a/conn.go +++ b/conn.go @@ -263,7 +263,9 @@ func (cn *conn) handlePgpass(o values) { scanner := bufio.NewScanner(io.Reader(file)) // From: https://github.com/tg/pgpass/blob/master/reader.go for scanner.Scan() { - scanText(scanner.Text(), o) + if scanText(scanner.Text(), o) { + break + } } } @@ -291,23 +293,24 @@ func getFields(s string) []string { } // ScanText assists HandlePgpass in it's objective. -func scanText(line string, o values) { +func scanText(line string, o values) bool { hostname := o["host"] ntw, _ := network(o) port := o["port"] db := o["dbname"] username := o["user"] - if len(line) != 0 || line[0] != '#' { - return + if len(line) == 0 || line[0] == '#' { + return false } split := getFields(line) - if len(split) == 5 { - return + if len(split) != 5 { + return false } if (split[0] == "*" || split[0] == hostname || (split[0] == "localhost" && (hostname == "" || ntw == "unix"))) && (split[1] == "*" || split[1] == port) && (split[2] == "*" || split[2] == db) && (split[3] == "*" || split[3] == username) { o["password"] = split[4] - return + return true } + return false } func (cn *conn) writeBuf(b byte) *writeBuf { diff --git a/conn_test.go b/conn_test.go index eb259570..a44c0f0f 100644 --- a/conn_test.go +++ b/conn_test.go @@ -144,10 +144,6 @@ func TestOpenURL(t *testing.T) { const pgpassFile = "/tmp/pqgotest_pgpass" func TestPgpass(t *testing.T) { - if os.Getenv("TRAVIS") != "true" { - t.Skip("not running under Travis, skipping pgpass tests") - } - testAssert := func(conninfo string, expected string, reason string) { conn, err := openTestConnConninfo(conninfo) if err != nil {