Skip to content

Incorrect whitespace handling when parsing key-value connection strings #2284

Description

@namasme

Describe the bug
The function parseKeywordValueSettings in the pgconn package incorrectly handles whitespace when parsing keyword/value settings. Specifically:

  1. It fails if the string has more than one trailing whitespace, as it expects another key-value pair after it.
  2. For quoted values, the behavior is inconsistent. The function does not consume a trailing space after a quoted value, instead relying on the next iteration's trimming. In other words, quoted values cannot be followed by trailing whitespaces at all.

This results in unexpected parsing failures for valid connection strings (according to the documentation and to psql's behaviour). The logic should be adjusted to allow trailing spaces without requiring another key-value pair.

I'd be happy to provide a fix for this if we agree on a strategy.

To Reproduce
Here's a minimal example to reproduce the issue:

package main

import (
	"context"
	"log"

	"github.com/jackc/pgx/v5/pgxpool"
)

func main() {
	_, err := pgxpool.New(context.Background(), "dbname=foo  ")
	log.Fatal(err)
}

You can also find a more detailed breakdown of the different ways to trigger the error here, isolating the root cause to the aforementioned function.

Expected behavior

parseKeywordValueSettings(`port=5432 host=foo  `) // map[string]string{"host":"foo", "port":"5432"}

Actual behavior

parseKeywordValueSettings(`port=5432 host=foo  `) // invalid keyword/value

Version

  • Go: $ go version -> go version go1.24.0 darwin/arm64
  • PostgreSQL: $ psql --no-psqlrc --tuples-only -c 'select version()' -> PostgreSQL 14.17 (Ubuntu 14.17-1.pgdg22.04+1) on aarch64-unknown-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, 64-bit
  • pgx: $ grep 'github.com/jackc/pgx/v[0-9]' go.mod -> github.com/jackc/pgx/v5 v5.7.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions