Make libpq reject non-numeric and out-of-range port numbers with a suitable
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 27 Sep 2009 03:43:10 +0000 (03:43 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 27 Sep 2009 03:43:10 +0000 (03:43 +0000)
error message, rather than blundering on and failing with something opaque.

Sam Mason

src/interfaces/libpq/fe-connect.c

index 38b382358682f53f0205558e5af69ccc29488d76..011ebab1d5e6eb082b6f095324438a670f2b88dd 100644 (file)
@@ -817,7 +817,16 @@ connectDBStart(PGconn *conn)
 
        /* Set up port number as a string */
        if (conn->pgport != NULL && conn->pgport[0] != '\0')
+       {
                portnum = atoi(conn->pgport);
+               if (portnum < 1 || portnum > 65535)
+               {
+                       appendPQExpBuffer(&conn->errorMessage,
+                                                         libpq_gettext("invalid port number: \"%s\"\n"),
+                                                         conn->pgport);
+                       goto connect_errReturn;
+               }
+       }
        else
                portnum = DEF_PGPORT;
        snprintf(portstr, sizeof(portstr), "%d", portnum);