[BUG FIX]Connection fails with whitespace after keepalives parameter value

Yuto Sasaki (Fujitsu) <sasaki.yuto-00@fujitsu.com>

From: "Yuto Sasaki (Fujitsu)" <sasaki.yuto-00@fujitsu.com>
To: "pgsql-hackers@lists.postgresql.org" <pgsql-hackers@lists.postgresql.org>
Date: 2024-10-01T05:11:21Z
Lists: pgsql-hackers

Attachments

Hi hackers,

I've discovered a bug in ECPG that causes database connection failures. This issue
appears to stem from libpq layer.

Found bug: The EXEC SQL CONNECT TO statement fails to connect to the database.
Specifically, the following code:

```c
EXEC SQL CONNECT TO tcp:postgresql://localhost:5432/postgres?keepalives=1
&keepalives_idle=1 USER yuto;
```

When precompiled and executed, this code fails to connect to the database.
Error message:

```
failed: keepalives parameter must be an integer
```

Steps to reproduce: The issue can be reproduced using the attached pgc file.

Root cause: The method for parsing the keepalives parameter in the useKeepalives
function of the libpq library is not appropriate. Specifically, it doesn't
account for whitespace following the numeric value.

Proposed fix: strtol() can be replaced with pqParseIntParam() like other paramters.
This skips whitespaces.

Concerns:
1. This fix may limit parameter values to integers only.
   Example: keepalives=2147483648 (values larger than INT_MAX can't be read)
2. Whitespace after '=' that was previously not read will now be read.
   Example: keepalives= 1 (previously couldn't be read, now read as 1)
3. This issue was introduced in previous versions (as far as I could verify,
   from 9.0), so we may need to consider backpatching if necessary.

These concerns warrant further discussion.

I'd appreciate your review of this patch and these concerns. Thank you.

Commits

  1. libpq: Discard leading and trailing spaces for parameters and values in URIs

  2. ecpg: avoid adding whitespace around '&' in connection URLs.

  3. Parse libpq's "keepalives" option more like other integer options.