Skip to content

Commit babf38e

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 9411446 commit babf38e

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
@@ -9125,18 +9125,59 @@ flatten_reloptions(Oid relid)
91259125
Anum_pg_class_reloptions, &isnull);
91269126
if (!isnull)
91279127
{
9128-
Datum sep,
9129-
txt;
9128+
StringInfoData buf;
9129+
Datum *options;
9130+
int noptions;
9131+
int i;
91309132

9131-
/*
9132-
* We want to use array_to_text(reloptions, ', ') --- but
9133-
* DirectFunctionCall2(array_to_text) does not work, because
9134-
* array_to_text() relies on flinfo to be valid. So use
9135-
* OidFunctionCall2.
9136-
*/
9137-
sep = CStringGetTextDatum(", ");
9138-
txt = OidFunctionCall2(F_ARRAY_TO_TEXT, reloptions, sep);
9139-
result = TextDatumGetCString(txt);
9133+
initStringInfo(&buf);
9134+
9135+
deconstruct_array(DatumGetArrayTypeP(reloptions),
9136+
TEXTOID, -1, false, 'i',
9137+
&options, NULL, &noptions);
9138+
9139+
for (i = 0; i < noptions; i++)
9140+
{
9141+
char *option = TextDatumGetCString(options[i]);
9142+
char *name;
9143+
char *separator;
9144+
char *value;
9145+
9146+
/*
9147+
* Each array element should have the form name=value. If the "="
9148+
* is missing for some reason, treat it like an empty value.
9149+
*/
9150+
name = option;
9151+
separator = strchr(option, '=');
9152+
if (separator)
9153+
{
9154+
*separator = '\0';
9155+
value = separator + 1;
9156+
}
9157+
else
9158+
value = "";
9159+
9160+
if (i > 0)
9161+
appendStringInfoString(&buf, ", ");
9162+
appendStringInfo(&buf, "%s=", quote_identifier(name));
9163+
9164+
/*
9165+
* In general we need to quote the value; but to avoid unnecessary
9166+
* clutter, do not quote if it is an identifier that would not
9167+
* need quoting. (We could also allow numbers, but that is a bit
9168+
* trickier than it looks --- for example, are leading zeroes
9169+
* significant? We don't want to assume very much here about what
9170+
* custom reloptions might mean.)
9171+
*/
9172+
if (quote_identifier(value) == value)
9173+
appendStringInfoString(&buf, value);
9174+
else
9175+
simple_quote_literal(&buf, value);
9176+
9177+
pfree(option);
9178+
}
9179+
9180+
result = buf.data;
91409181
}
91419182

91429183
ReleaseSysCache(tuple);

0 commit comments

Comments
 (0)