Skip to content

pgconn: fix parseKeywordValueSettings rejecting trailing whitespace#2532

Merged
jackc merged 1 commit into
jackc:masterfrom
alliasgher:fix-kv-trailing-whitespace
Apr 25, 2026
Merged

pgconn: fix parseKeywordValueSettings rejecting trailing whitespace#2532
jackc merged 1 commit into
jackc:masterfrom
alliasgher:fix-kv-trailing-whitespace

Conversation

@alliasgher
Copy link
Copy Markdown
Contributor

Summary

Fixes #2284.

parseKeywordValueSettings rejected valid keyword/value connection strings that had trailing whitespace or multiple spaces between values.

Root cause

After consuming each value, the parser advanced by one byte (s[end+1:] for unquoted values, s[end+1:] after the closing quote for quoted values). This skipped a single whitespace character but left any additional leading spaces in s. On the next iteration, the loop found no = in " " and returned "invalid keyword/value".

The same issue occurred with quoted values followed by any trailing whitespace, because s[end+1:] only moved past the closing ' and left the spaces.

Fix

  • Trim leading whitespace from s once before entering the loop so that an entirely-whitespace input exits cleanly.
  • After consuming each value (unquoted and quoted), use strings.TrimLeft(…) instead of [end+1:] to consume all subsequent whitespace. This matches libpq's behavior and the PostgreSQL documentation, which describes the connection string syntax as allowing spaces between key-value pairs.

Tests added

TestParseConfigKVTrailingWhitespace covers:

  • single / multiple trailing spaces
  • trailing tab
  • quoted value followed by trailing spaces
  • two key-value pairs with trailing spaces

The key-value connection string parser failed with "invalid keyword/value"
whenever the string ended with more than one space, or when a quoted value
was followed by any trailing spaces. This is because after consuming each
value the code only stripped a single byte (s[end+1:]), leaving extra
leading whitespace that caused the next loop iteration to see a partial
string without an '=' character.

Fix by trimming all leading whitespace from the remainder of the string
after consuming each value (unquoted and quoted), and also trimming the
string once before entering the loop. This matches libpq's behaviour,
which accepts any amount of whitespace between and after key-value pairs.

Fixes jackc#2284

Signed-off-by: alliasgher <alliasgher123@gmail.com>
@jackc jackc merged commit 53941bc into jackc:master Apr 25, 2026
13 of 14 checks passed
@jackc
Copy link
Copy Markdown
Owner

jackc commented Apr 25, 2026

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect whitespace handling when parsing key-value connection strings

2 participants