Commit 7454515e authored by Tom Lane's avatar Tom Lane

Fix mistakes in pg_ctl's code for "start -w" that tries to cope with

non-default settings for the postmaster's port number.  The code to parse
command line options and postgresql.conf entries wasn't quite right about
whitespace or quotes, and it was coded in a not-very-readable way too.
Per bug #3969 from Itagaki Takahiro, though this is more extensive than his
proposed patch (which fixed only the whitespace problem).
This code has been broken since it was put in in 8.0, so patch all the way
back.
parent 5ce6829b
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* *
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.92 2008/01/01 19:45:55 momjian Exp $ * $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.93 2008/02/20 22:18:15 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -49,8 +49,6 @@ int optreset; ...@@ -49,8 +49,6 @@ int optreset;
typedef long pgpid_t; typedef long pgpid_t;
#define WHITESPACE "\f\n\r\t\v" /* as defined by isspace() */
/* postgres version ident string */ /* postgres version ident string */
#define PM_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n" #define PM_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"
...@@ -416,33 +414,52 @@ test_postmaster_connection(bool do_checkpoint) ...@@ -416,33 +414,52 @@ test_postmaster_connection(bool do_checkpoint)
int i; int i;
char portstr[32]; char portstr[32];
char *p; char *p;
char *q;
char connstr[128]; /* Should be way more than enough! */ char connstr[128]; /* Should be way more than enough! */
*portstr = '\0'; *portstr = '\0';
/* post_opts */ /*
* Look in post_opts for a -p switch.
*
* This parsing code is not amazingly bright; it could for instance
* get fooled if ' -p' occurs within a quoted argument value. Given
* that few people pass complicated settings in post_opts, it's
* probably good enough.
*/
for (p = post_opts; *p;) for (p = post_opts; *p;)
{ {
/* advance past whitespace/quoting */ /* advance past whitespace */
while (isspace((unsigned char) *p) || *p == '\'' || *p == '"') while (isspace((unsigned char) *p))
p++; p++;
if (strncmp(p, "-p", strlen("-p")) == 0) if (strncmp(p, "-p", 2) == 0)
{ {
p += strlen("-p"); p += 2;
/* advance past whitespace/quoting */ /* advance past any whitespace/quoting */
while (isspace((unsigned char) *p) || *p == '\'' || *p == '"') while (isspace((unsigned char) *p) || *p == '\'' || *p == '"')
p++; p++;
strlcpy(portstr, p, Min(strcspn(p, "\"'" WHITESPACE) + 1, /* find end of value (not including any ending quote!) */
sizeof(portstr))); q = p;
while (*q &&
!(isspace((unsigned char) *q) || *q == '\'' || *q == '"'))
q++;
/* and save the argument value */
strlcpy(portstr, p, Min((q - p) + 1, sizeof(portstr)));
/* keep looking, maybe there is another -p */ /* keep looking, maybe there is another -p */
p = q;
} }
/* Advance to next whitespace */ /* Advance to next whitespace */
while (*p && !isspace((unsigned char) *p)) while (*p && !isspace((unsigned char) *p))
p++; p++;
} }
/* config file */ /*
* Search config file for a 'port' option.
*
* This parsing code isn't amazingly bright either, but it should be
* okay for valid port settings.
*/
if (!*portstr) if (!*portstr)
{ {
char **optlines; char **optlines;
...@@ -456,28 +473,35 @@ test_postmaster_connection(bool do_checkpoint) ...@@ -456,28 +473,35 @@ test_postmaster_connection(bool do_checkpoint)
while (isspace((unsigned char) *p)) while (isspace((unsigned char) *p))
p++; p++;
if (strncmp(p, "port", strlen("port")) != 0) if (strncmp(p, "port", 4) != 0)
continue; continue;
p += strlen("port"); p += 4;
while (isspace((unsigned char) *p)) while (isspace((unsigned char) *p))
p++; p++;
if (*p != '=') if (*p != '=')
continue; continue;
p++; p++;
while (isspace((unsigned char) *p)) /* advance past any whitespace/quoting */
while (isspace((unsigned char) *p) || *p == '\'' || *p == '"')
p++; p++;
strlcpy(portstr, p, Min(strcspn(p, "#" WHITESPACE) + 1, /* find end of value (not including any ending quote/comment!) */
sizeof(portstr))); q = p;
while (*q &&
!(isspace((unsigned char) *q) ||
*q == '\'' || *q == '"' || *q == '#'))
q++;
/* and save the argument value */
strlcpy(portstr, p, Min((q - p) + 1, sizeof(portstr)));
/* keep looking, maybe there is another */ /* keep looking, maybe there is another */
} }
} }
} }
/* environment */ /* Check environment */
if (!*portstr && getenv("PGPORT") != NULL) if (!*portstr && getenv("PGPORT") != NULL)
strlcpy(portstr, getenv("PGPORT"), sizeof(portstr)); strlcpy(portstr, getenv("PGPORT"), sizeof(portstr));
/* default */ /* Else use compiled-in default */
if (!*portstr) if (!*portstr)
snprintf(portstr, sizeof(portstr), "%d", DEF_PGPORT); snprintf(portstr, sizeof(portstr), "%d", DEF_PGPORT);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment