lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
9.04k stars 909 forks source link

v1.10.8 break if .pgpass contains empty line #1119

Closed acoshift closed 1 year ago

acoshift commented 1 year ago

After I upgrade lib/pq from v1.10.7 to v1.10.8. It's panic every query command with message

runtime error: index out of range [0] with length 0

After investigate I found that if .pgpass contains empty line, It'll error.

Step to reproduce

  1. Create ~/.pgpass with this content
localhost:5432:*:postgres:password

localhost:5432:*:user:password
  1. Set .pgpass permission
$ chmod 600 ~/.pgpass
  1. Run this Go code
package main

import (
    "database/sql"
    "log"

    _ "github.com/lib/pq"
)

func main() {
    db, err := sql.Open("postgres", "postgres://postgres@localhost/postgres?sslmode=disable")
    if err != nil {
        log.Fatalf("Error opening database: %v", err)
    }
    defer db.Close()

    var n int
    err = db.QueryRow(`select 1`).Scan(&n)
    if err != nil {
        log.Fatalf("Error querying database: %v", err)
    }
    log.Printf("n=%d", n)
}
go run .
2023/04/16 15:53:49 Error querying database: runtime error: index out of range [0] with length 0
exit status 1
KumanekoSakura commented 1 year ago

@otan I upgraded from 1.10.7 to 1.10.8, and found that .pgpass file is no longer processed (regardless of whether .pgpass contains an invalid line). Like #1120 suggests,

if len(line) != 0 || line[0] != '#' {
    return
}

is by error always true, and

if len(split) == 5 {
    return
}

is by error (as long as line is valid) always true.

Please fix this regression introduced by #1101 and release immediately, or more people will be confused.