Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbabf38e

Browse files
committed
Teach flatten_reloptions() to quote option values safely.
flatten_reloptions() supposed that it didn't really need to do anythingbeyond inserting commas between reloption array elements. However, inprinciple the value of a reloption could be nearly anything, since thegrammar allows a quoted string there. Any restrictions on it would comefrom validity checking appropriate to the particular option, if any.A reloption value that isn't a simple identifier or number could thus leadto dump/reload failures due to syntax errors in CREATE statements issuedby pg_dump. We've gotten away with not worrying about this so far withthe core-supported reloptions, but extensions might allow reloption valuesthat cause trouble, as in bug #13840 from Kouhei Sutou.To fix, split the reloption array elements explicitly, and then convertany 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 safeand requires little code.) While we're at it, also quote reloption namesif they're not safe identifiers; that may not be a likely problem in thefield, but we might as well try to be bulletproof here.It's been like this for a long time, so back-patch to all supportedbranches.Kouhei Sutou, adjusted some by me
1 parent9411446 commitbabf38e

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-
Datumsep,
9129-
txt;
9128+
StringInfoDatabuf;
9129+
Datum*options;
9130+
intnoptions;
9131+
inti;
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)

[8]ページ先頭

©2009-2025 Movatter.jp