Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4f4061b

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 ea9e06a commit 4f4061b

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
@@ -1687,7 +1687,7 @@ useKeepalives(PGconn *conn)
16871687
/*
16881688
* Parse and try to interpret "value" as an integer value, and if successful,
16891689
* store it in *result, complaining if there is any trailing garbage or an
1690-
* overflow.
1690+
* overflow. This allows any number of leading and trailing whitespaces.
16911691
*/
16921692
static bool
16931693
parse_int_param(const char *value, int *result, PGconn *conn,
@@ -1698,14 +1698,31 @@ parse_int_param(const char *value, int *result, PGconn *conn,
16981698

16991699
*result = 0;
17001700

1701+
/* strtol(3) skips leading whitespaces */
17011702
errno = 0;
17021703
numval = strtol(value, &end, 10);
1703-
if (errno == 0 && *end == '\0' && numval == (int) numval)
1704-
{
1705-
*result = numval;
1706-
return true;
1707-
}
17081704

1705+
/*
1706+
* If no progress was done during the parsing or an error happened, fail.
1707+
* This tests properly for overflows of the result.
1708+
*/
1709+
if (value == end || errno != 0 || numval != (int) numval)
1710+
goto error;
1711+
1712+
/*
1713+
* Skip any trailing whitespace; if anything but whitespace remains before
1714+
* the terminating character, fail
1715+
*/
1716+
while (*end && *end != '\0' && isspace((unsigned char) *end))
1717+
end++;
1718+
1719+
if (*end && *end != '\0')
1720+
goto error;
1721+
1722+
*result = numval;
1723+
return true;
1724+
1725+
error:
17091726
appendPQExpBuffer(&conn->errorMessage,
17101727
libpq_gettext("invalid integer value \"%s\" for connection option \"%s\"\n"),
17111728
value, context);

0 commit comments

Comments
 (0)