diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2008-02-20 22:18:35 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2008-02-20 22:18:35 +0000 |
commit | f6b6c2420ee0043c07fd9d74f1d86b24399b80b2 (patch) | |
tree | 6440087d682a5da99262509eae6ef9726e6d7704 /src | |
parent | 63df2c788a25076b645176727d7a4a02061881f4 (diff) | |
download | postgresql-f6b6c2420ee0043c07fd9d74f1d86b24399b80b2.tar.gz postgresql-f6b6c2420ee0043c07fd9d74f1d86b24399b80b2.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/pg_ctl/pg_ctl.c | 62 |
1 files changed, 43 insertions, 19 deletions
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index fc5061e044a..ded748d2c11 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -4,7 +4,7 @@ * * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.61.2.3 2006/06/25 04:38:00 alvherre Exp $ + * $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.61.2.4 2008/02/20 22:18:35 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -36,8 +36,6 @@ int optreset; typedef long pgpid_t; -#define WHITESPACE "\f\n\r\t\v" /* as defined by isspace() */ - /* postmaster version ident string */ #define PM_VERSIONSTR "postmaster (PostgreSQL) " PG_VERSION "\n" @@ -374,32 +372,51 @@ test_postmaster_connection(void) int i; char portstr[32]; char *p; + char *q; *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;) { - /* advance past whitespace/quoting */ - while (isspace((unsigned char) *p) || *p == '\'' || *p == '"') + /* advance past whitespace */ + while (isspace((unsigned char) *p)) p++; - if (strncmp(p, "-p", strlen("-p")) == 0) + if (strncmp(p, "-p", 2) == 0) { - p += strlen("-p"); - /* advance past whitespace/quoting */ + p += 2; + /* advance past any whitespace/quoting */ while (isspace((unsigned char) *p) || *p == '\'' || *p == '"') p++; - StrNCpy(portstr, p, Min(strcspn(p, "\"'" WHITESPACE) + 1, - sizeof(portstr))); + /* find end of value (not including any ending quote!) */ + q = p; + while (*q && + !(isspace((unsigned char) *q) || *q == '\'' || *q == '"')) + q++; + /* and save the argument value */ + StrNCpy(portstr, p, Min((q - p) + 1, sizeof(portstr))); /* keep looking, maybe there is another -p */ + p = q; } /* Advance to next whitespace */ while (*p && !isspace((unsigned char) *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) { char **optlines; @@ -413,28 +430,35 @@ test_postmaster_connection(void) while (isspace((unsigned char) *p)) p++; - if (strncmp(p, "port", strlen("port")) != 0) + if (strncmp(p, "port", 4) != 0) continue; - p += strlen("port"); + p += 4; while (isspace((unsigned char) *p)) p++; if (*p != '=') continue; p++; - while (isspace((unsigned char) *p)) + /* advance past any whitespace/quoting */ + while (isspace((unsigned char) *p) || *p == '\'' || *p == '"') p++; - StrNCpy(portstr, p, Min(strcspn(p, "#" WHITESPACE) + 1, - sizeof(portstr))); + /* find end of value (not including any ending quote/comment!) */ + q = p; + while (*q && + !(isspace((unsigned char) *q) || + *q == '\'' || *q == '"' || *q == '#')) + q++; + /* and save the argument value */ + StrNCpy(portstr, p, Min((q - p) + 1, sizeof(portstr))); /* keep looking, maybe there is another */ } } } - /* environment */ + /* Check environment */ if (!*portstr && getenv("PGPORT") != NULL) StrNCpy(portstr, getenv("PGPORT"), sizeof(portstr)); - /* default */ + /* Else use compiled-in default */ if (!*portstr) snprintf(portstr, sizeof(portstr), "%d", DEF_PGPORT); |