return val != 0 ? 1 : 0;
 }
 
+/*
+ * Parse and try to interpret "value" as an integer value, and if successful,
+ * store it in *result, complaining if there is any trailing garbage or an
+ * overflow.
+ */
+static bool
+parse_int_param(const char *value, int *result, PGconn *conn,
+                               const char *context)
+{
+       char       *end;
+       long            numval;
+
+       *result = 0;
+
+       errno = 0;
+       numval = strtol(value, &end, 10);
+       if (errno == 0 && *end == '\0' && numval == (int) numval)
+       {
+               *result = numval;
+               return true;
+       }
+
+       appendPQExpBuffer(&conn->errorMessage,
+                                         libpq_gettext("invalid integer value \"%s\" for keyword \"%s\"\n"),
+                                         value, context);
+       return false;
+}
+
 #ifndef WIN32
 /*
  * Set the keepalive idle timer.
        if (conn->keepalives_idle == NULL)
                return 1;
 
-       idle = atoi(conn->keepalives_idle);
+       if (!parse_int_param(conn->keepalives_idle, &idle, conn,
+                                                "keepalives_idle"))
+               return 0;
        if (idle < 0)
                idle = 0;
 
        if (conn->keepalives_interval == NULL)
                return 1;
 
-       interval = atoi(conn->keepalives_interval);
+       if (!parse_int_param(conn->keepalives_interval, &interval, conn,
+                                                "keepalives_interval"))
+               return 0;
        if (interval < 0)
                interval = 0;
 
        if (conn->keepalives_count == NULL)
                return 1;
 
-       count = atoi(conn->keepalives_count);
+       if (!parse_int_param(conn->keepalives_count, &count, conn,
+                                                "keepalives_count"))
+               return 0;
        if (count < 0)
                count = 0;
 
        int                     idle = 0;
        int                     interval = 0;
 
-       if (conn->keepalives_idle)
-               idle = atoi(conn->keepalives_idle);
+       if (conn->keepalives_idle &&
+               !parse_int_param(conn->keepalives_idle, &idle, conn,
+                                                "keepalives_idle"))
+               return 0;
        if (idle <= 0)
                idle = 2 * 60 * 60;             /* 2 hours = default */
 
-       if (conn->keepalives_interval)
-               interval = atoi(conn->keepalives_interval);
+       if (conn->keepalives_interval &&
+               !parse_int_param(conn->keepalives_interval, &interval, conn,
+                                                "keepalives_interval"))
+               return 0;
        if (interval <= 0)
                interval = 1;                   /* 1 second = default */
 
         */
        if (conn->connect_timeout != NULL)
        {
-               timeout = atoi(conn->connect_timeout);
+               if (!parse_int_param(conn->connect_timeout, &timeout, conn,
+                                                        "connect_timeout"))
+                       return 0;
+
                if (timeout > 0)
                {
                        /*
                        if (timeout < 2)
                                timeout = 2;
                }
+               else                                    /* negative means 0 */
+                       timeout = 0;
        }
 
        for (;;)
                        thisport = DEF_PGPORT;
                else
                {
-                       thisport = atoi(ch->port);
+                       if (!parse_int_param(ch->port, &thisport, conn, "port"))
+                               goto error_return;
+
                        if (thisport < 1 || thisport > 65535)
                        {
                                appendPQExpBuffer(&conn->errorMessage,