Skip to content

Commit 404c45b

Browse files
committed
Teach flatten_reloptions() to quote option values safely.
flatten_reloptions() supposed that it didn't really need to do anything beyond inserting commas between reloption array elements. However, in principle the value of a reloption could be nearly anything, since the grammar allows a quoted string there. Any restrictions on it would come from validity checking appropriate to the particular option, if any. A reloption value that isn't a simple identifier or number could thus lead to dump/reload failures due to syntax errors in CREATE statements issued by pg_dump. We've gotten away with not worrying about this so far with the core-supported reloptions, but extensions might allow reloption values that cause trouble, as in bug #13840 from Kouhei Sutou. To fix, split the reloption array elements explicitly, and then convert any value that doesn't look like a safe identifier to a string literal. (The details of the quoting rule could be debated, but this way is safe and requires little code.) While we're at it, also quote reloption names if they're not safe identifiers; that may not be a likely problem in the field, but we might as well try to be bulletproof here. It's been like this for a long time, so back-patch to all supported branches. Kouhei Sutou, adjusted some by me
1 parent d932391 commit 404c45b

File tree

1 file changed

+52
-11
lines changed

1 file changed

+52
-11
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9856,18 +9856,59 @@ flatten_reloptions(Oid relid)
98569856
Anum_pg_class_reloptions, &isnull);
98579857
if (!isnull)
98589858
{
9859-
Datum sep,
9860-
txt;
9859+
StringInfoData buf;
9860+
Datum *options;
9861+
int noptions;
9862+
int i;
98619863

9862-
/*
9863-
* We want to use array_to_text(reloptions, ', ') --- but
9864-
* DirectFunctionCall2(array_to_text) does not work, because
9865-
* array_to_text() relies on flinfo to be valid. So use
9866-
* OidFunctionCall2.
9867-
*/
9868-
sep = CStringGetTextDatum(", ");
9869-
txt = OidFunctionCall2(F_ARRAY_TO_TEXT, reloptions, sep);
9870-
result = TextDatumGetCString(txt);
9864+
initStringInfo(&buf);
9865+
9866+
deconstruct_array(DatumGetArrayTypeP(reloptions),
9867+
TEXTOID, -1, false, 'i',
9868+
&options, NULL, &noptions);
9869+
9870+
for (i = 0; i < noptions; i++)
9871+
{
9872+
char *option = TextDatumGetCString(options[i]);
9873+
char *name;
9874+
char *separator;
9875+
char *value;
9876+
9877+
/*
9878+
* Each array element should have the form name=value. If the "="
9879+
* is missing for some reason, treat it like an empty value.
9880+
*/
9881+
name = option;
9882+
separator = strchr(option, '=');
9883+
if (separator)
9884+
{
9885+
*separator = '\0';
9886+
value = separator + 1;
9887+
}
9888+
else
9889+
value = "";
9890+
9891+
if (i > 0)
9892+
appendStringInfoString(&buf, ", ");
9893+
appendStringInfo(&buf, "%s=", quote_identifier(name));
9894+
9895+
/*
9896+
* In general we need to quote the value; but to avoid unnecessary
9897+
* clutter, do not quote if it is an identifier that would not
9898+
* need quoting. (We could also allow numbers, but that is a bit
9899+
* trickier than it looks --- for example, are leading zeroes
9900+
* significant? We don't want to assume very much here about what
9901+
* custom reloptions might mean.)
9902+
*/
9903+
if (quote_identifier(value) == value)
9904+
appendStringInfoString(&buf, value);
9905+
else
9906+
simple_quote_literal(&buf, value);
9907+
9908+
pfree(option);
9909+
}
9910+
9911+
result = buf.data;
98719912
}
98729913

98739914
ReleaseSysCache(tuple);

0 commit comments

Comments
 (0)