Skip to content

Commit 2b0f959

Browse files
committed
Fix parsing of integer values for connection parameters in libpq
Commit e7a2217 has introduced stricter checks for integer values in connection parameters for libpq. However this failed to correctly check after trailing whitespaces, while leading whitespaces were discarded per the use of strtol(3). This fixes and refactors the parsing logic to handle both cases consistently. Note that trying to restrict the use of trailing whitespaces can easily break connection strings like in ECPG regression tests (these have allowed me to catch the parsing bug with connect_timeout). Author: Michael Paquier Reviewed-by: Lars Kanis Discussion: https://postgr.es/m/a9b4cbd7-4ecb-06b2-ebd7-1739bbff3217@greiz-reinsdorf.de Backpatch-through: 12
1 parent ef13f91 commit 2b0f959

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,7 @@ useKeepalives(PGconn *conn)
16541654
/*
16551655
* Parse and try to interpret "value" as an integer value, and if successful,
16561656
* store it in *result, complaining if there is any trailing garbage or an
1657-
* overflow.
1657+
* overflow. This allows any number of leading and trailing whitespaces.
16581658
*/
16591659
static bool
16601660
parse_int_param(const char *value, int *result, PGconn *conn,
@@ -1665,14 +1665,31 @@ parse_int_param(const char *value, int *result, PGconn *conn,
16651665

16661666
*result = 0;
16671667

1668+
/* strtol(3) skips leading whitespaces */
16681669
errno = 0;
16691670
numval = strtol(value, &end, 10);
1670-
if (errno == 0 && *end == '\0' && numval == (int) numval)
1671-
{
1672-
*result = numval;
1673-
return true;
1674-
}
16751671

1672+
/*
1673+
* If no progress was done during the parsing or an error happened, fail.
1674+
* This tests properly for overflows of the result.
1675+
*/
1676+
if (value == end || errno != 0 || numval != (int) numval)
1677+
goto error;
1678+
1679+
/*
1680+
* Skip any trailing whitespace; if anything but whitespace remains before
1681+
* the terminating character, fail
1682+
*/
1683+
while (*end && *end != '\0' && isspace((unsigned char) *end))
1684+
end++;
1685+
1686+
if (*end && *end != '\0')
1687+
goto error;
1688+
1689+
*result = numval;
1690+
return true;
1691+
1692+
error:
16761693
appendPQExpBuffer(&conn->errorMessage,
16771694
libpq_gettext("invalid integer value \"%s\" for connection option \"%s\"\n"),
16781695
value, context);

0 commit comments

Comments
 (0)