Cause fmtId to always use its internal buffer for the returned value,

in hopes of making erroneous usage more apparent.  Per discussion 15-Apr.
This commit is contained in:
Tom Lane 2002-05-06 17:34:45 +00:00
parent 1cf693ab86
commit 340b66cc70
1 changed files with 33 additions and 28 deletions

View File

@ -15,7 +15,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.44 2002/04/24 14:03:22 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.45 2002/05/06 17:34:45 tgl Exp $
* *
* Modifications - 28-Jun-2000 - pjw@rhyme.com.au * Modifications - 28-Jun-2000 - pjw@rhyme.com.au
* *
@ -2069,11 +2069,12 @@ _reconnectAsOwner(ArchiveHandle *AH, const char *dbname, TocEntry *te)
/* /*
* fmtId * fmtId
* *
* checks input string for non-lowercase characters * Quotes input string if it's not a legitimate SQL identifier as-is,
* returns pointer to input string or string surrounded by double quotes * or all the time if force_quotes is true.
* *
* Note that the returned string should be used immediately since it * Note that the returned string must be used before calling fmtId again,
* uses a static buffer to hold the string. Non-reentrant but faster? * since we re-use the same return buffer each time. Non-reentrant but
* avoids memory leakage.
*/ */
const char * const char *
fmtId(const char *rawid, bool force_quotes) fmtId(const char *rawid, bool force_quotes)
@ -2081,13 +2082,19 @@ fmtId(const char *rawid, bool force_quotes)
static PQExpBuffer id_return = NULL; static PQExpBuffer id_return = NULL;
const char *cp; const char *cp;
if (id_return) /* first time through? */
resetPQExpBuffer(id_return);
else
id_return = createPQExpBuffer();
if (!force_quotes) if (!force_quotes)
{ {
/* do a quick check on the first character... */ /* do a quick check on the first character... */
if (!islower((unsigned char) *rawid)) if (!islower((unsigned char) *rawid) && *rawid != '_')
force_quotes = true; force_quotes = true;
/* otherwise check the entire string */
else else
{
/* otherwise check the entire string */
for (cp = rawid; *cp; cp++) for (cp = rawid; *cp; cp++)
{ {
if (!(islower((unsigned char) *cp) || if (!(islower((unsigned char) *cp) ||
@ -2099,15 +2106,15 @@ fmtId(const char *rawid, bool force_quotes)
} }
} }
} }
}
if (!force_quotes) if (!force_quotes)
return rawid; /* no quoting needed */ {
/* no quoting needed */
if (id_return) appendPQExpBufferStr(id_return, rawid);
resetPQExpBuffer(id_return); }
else else
id_return = createPQExpBuffer(); {
appendPQExpBufferChar(id_return, '\"'); appendPQExpBufferChar(id_return, '\"');
for (cp = rawid; *cp; cp++) for (cp = rawid; *cp; cp++)
{ {
@ -2117,13 +2124,11 @@ fmtId(const char *rawid, bool force_quotes)
* backslash/double-quote pair. - thomas 2000-08-05 * backslash/double-quote pair. - thomas 2000-08-05
*/ */
if (*cp == '\"') if (*cp == '\"')
{
appendPQExpBufferChar(id_return, '\"'); appendPQExpBufferChar(id_return, '\"');
appendPQExpBufferChar(id_return, '\"');
}
appendPQExpBufferChar(id_return, *cp); appendPQExpBufferChar(id_return, *cp);
} }
appendPQExpBufferChar(id_return, '\"'); appendPQExpBufferChar(id_return, '\"');
}
return id_return->data; return id_return->data;
} }