Skip to content

Commit c7e27be

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 3c93a60 commit c7e27be

File tree

1 file changed

+52
-11
lines changed

1 file changed

+52
-11
lines changed

src/backend/utils/adt/ruleutils.c

+52-11
Original file line numberDiff line numberDiff line change
@@ -9858,18 +9858,59 @@ flatten_reloptions(Oid relid)
98589858
Anum_pg_class_reloptions, &isnull);
98599859
if (!isnull)
98609860
{
9861-
Datum sep,
9862-
txt;
9861+
StringInfoData buf;
9862+
Datum *options;
9863+
int noptions;
9864+
int i;
98639865

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

98759916
ReleaseSysCache(tuple);

0 commit comments

Comments
 (0)