Skip to content

Commit f9b3b3f

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 76eccf0 commit f9b3b3f

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
@@ -9396,18 +9396,59 @@ flatten_reloptions(Oid relid)
93969396
Anum_pg_class_reloptions, &isnull);
93979397
if (!isnull)
93989398
{
9399-
Datum sep,
9400-
txt;
9399+
StringInfoData buf;
9400+
Datum *options;
9401+
int noptions;
9402+
int i;
94019403

9402-
/*
9403-
* We want to use array_to_text(reloptions, ', ') --- but
9404-
* DirectFunctionCall2(array_to_text) does not work, because
9405-
* array_to_text() relies on flinfo to be valid. So use
9406-
* OidFunctionCall2.
9407-
*/
9408-
sep = CStringGetTextDatum(", ");
9409-
txt = OidFunctionCall2(F_ARRAY_TO_TEXT, reloptions, sep);
9410-
result = TextDatumGetCString(txt);
9404+
initStringInfo(&buf);
9405+
9406+
deconstruct_array(DatumGetArrayTypeP(reloptions),
9407+
TEXTOID, -1, false, 'i',
9408+
&options, NULL, &noptions);
9409+
9410+
for (i = 0; i < noptions; i++)
9411+
{
9412+
char *option = TextDatumGetCString(options[i]);
9413+
char *name;
9414+
char *separator;
9415+
char *value;
9416+
9417+
/*
9418+
* Each array element should have the form name=value. If the "="
9419+
* is missing for some reason, treat it like an empty value.
9420+
*/
9421+
name = option;
9422+
separator = strchr(option, '=');
9423+
if (separator)
9424+
{
9425+
*separator = '\0';
9426+
value = separator + 1;
9427+
}
9428+
else
9429+
value = "";
9430+
9431+
if (i > 0)
9432+
appendStringInfoString(&buf, ", ");
9433+
appendStringInfo(&buf, "%s=", quote_identifier(name));
9434+
9435+
/*
9436+
* In general we need to quote the value; but to avoid unnecessary
9437+
* clutter, do not quote if it is an identifier that would not
9438+
* need quoting. (We could also allow numbers, but that is a bit
9439+
* trickier than it looks --- for example, are leading zeroes
9440+
* significant? We don't want to assume very much here about what
9441+
* custom reloptions might mean.)
9442+
*/
9443+
if (quote_identifier(value) == value)
9444+
appendStringInfoString(&buf, value);
9445+
else
9446+
simple_quote_literal(&buf, value);
9447+
9448+
pfree(option);
9449+
}
9450+
9451+
result = buf.data;
94119452
}
94129453

94139454
ReleaseSysCache(tuple);

0 commit comments

Comments
 (0)