From: chenhj <chjischj@163.com>
To: pgsql-bugs@postgresql.org
Subject: 32 bit libpq fail to connecting when set a very large "connect_timeout" value
Date: Mon, 20 Oct 2014 14:01:03 +0800 (CST)
Message-ID: <75f4618f.c0f7.1492c250468.Coremail.chjischj@163.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Hi,all
This a bug report.
1)The problem
When set a very large value(such as 2147483647 which is the maximum of int ) to connect_timeout,and then connect to the backend using 32 bit psql, a timeout will occur.
For example:
[chenhj@node2 ~]$ export PGCONNECT_TIMEOUT=2147483647
[chenhj@node2 ~]$ psql
psql: timeout expired
While it's OK when using 64 bit psql.
[chenhj@node2 ~]$ export PGCONNECT_TIMEOUT=2147483647
[chenhj@node2 ~]$ psql
psql (9.2.8)
Type "help" for help.
postgres=# \q
2)The reason
I looked into the source code, and found the reason is a data overflow when calculating connecting's finish_time as the following.
src\interfaces\libpq\fe-connect.c:
static int
connectDBComplete(PGconn *conn)
{
PostgresPollingStatusType flag = PGRES_POLLING_WRITING;
time_tfinish_time = ((time_t) -1);
if (conn == NULL || conn->status == CONNECTION_BAD)
return 0;
/*
* Set up a time limit, if connect_timeout isn't zero.
*/
if (conn->connect_timeout != NULL)
{
inttimeout = atoi(conn->connect_timeout);
if (timeout > 0)
{
/*
* Rounding could cause connection to fail; need at least 2 secs
*/
if (timeout < 2)
timeout = 2;
/* calculate the finish time based on start + timeout */
finish_time = time(NULL) + timeout;// In 32 bit application time_t is a 32 bit, when timeout is very large, data overflow will occur and finish_time became a negative value.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
}
...
}
Of course this is a infrequence bug, but i think it should be fixed, such as the following(may be there's better way to fix it).
finish_time = time(NULL) + timeout;
==>
finish_time = time(NULL) + timeout;
if(finish_time < ((time_t) 0))
{
finish_time = ((time_t) -1);
}
Best Regards
Chen Huajun
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: chjischj@163.com
Subject: Re: 32 bit libpq fail to connecting when set a very large "connect_timeout" value
In-Reply-To: <75f4618f.c0f7.1492c250468.Coremail.chjischj@163.com>
References: <75f4618f.c0f7.1492c250468.Coremail.chjischj@163.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox