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

Commit8999f5e

Browse files
committed
Fix write/read of empty string fields in Nodes.
Historically, outToken has represented both NULL and empty-stringstrings as "<>", which readfuncs.c then read as NULL, thus failingto preserve empty-string fields accurately. Remarkably, this hasnot caused any serious problems yet, but let's fix it.We'll keep the "<>" notation for NULL, and use """" for empty string,because that matches other notational choices already in use.An actual input string of """" is converted to "\""" (this was truealready, apparently as a hangover from an ancient time when stringquoting was handled directly by pg_strtok).CHAR fields also use "<>", but for '\0'.Author: Tom Lane <tgl@sss.pgh.pa.us>Discussion:https://www.postgresql.org/message-id/flat/4159834.1657405226@sss.pgh.pa.us
1 parentaf51b2f commit8999f5e

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

‎src/backend/nodes/outfuncs.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,23 @@ static void outChar(StringInfo str, char c);
135135
* Convert an ordinary string (eg, an identifier) into a form that
136136
* will be decoded back to a plain token by read.c's functions.
137137
*
138-
* If a null or empty string is given, it is encoded as "<>".
138+
* If a null string pointer is given, it is encoded as '<>'.
139+
* An empty string is encoded as '""'. To avoid ambiguity, input
140+
* strings beginning with '<' or '"' receive a leading backslash.
139141
*/
140142
void
141143
outToken(StringInfostr,constchar*s)
142144
{
143-
if (s==NULL||*s=='\0')
145+
if (s==NULL)
144146
{
145147
appendStringInfoString(str,"<>");
146148
return;
147149
}
150+
if (*s=='\0')
151+
{
152+
appendStringInfoString(str,"\"\"");
153+
return;
154+
}
148155

149156
/*
150157
* Look for characters or patterns that are treated specially by read.c
@@ -178,6 +185,13 @@ outChar(StringInfo str, char c)
178185
{
179186
charin[2];
180187

188+
/* Traditionally, we've represented \0 as <>, so keep doing that */
189+
if (c=='\0')
190+
{
191+
appendStringInfoString(str,"<>");
192+
return;
193+
}
194+
181195
in[0]=c;
182196
in[1]='\0';
183197

@@ -636,7 +650,8 @@ _outString(StringInfo str, const String *node)
636650
{
637651
/*
638652
* We use outToken to provide escaping of the string's content, but we
639-
* don't want it to do anything with an empty string.
653+
* don't want it to convert an empty string to '""', because we're putting
654+
* double quotes around the string already.
640655
*/
641656
appendStringInfoChar(str,'"');
642657
if (node->sval[0]!='\0')

‎src/backend/nodes/readfuncs.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,18 @@
178178

179179
#definestrtobool(x) ((*(x) == 't') ? true : false)
180180

181-
#definenullable_string(token,length) \
182-
((length) == 0 ? NULL : debackslash(token, length))
181+
staticchar*
182+
nullable_string(constchar*token,intlength)
183+
{
184+
/* outToken emits <> for NULL, and pg_strtok makes that an empty string */
185+
if (length==0)
186+
returnNULL;
187+
/* outToken emits "" for empty string */
188+
if (length==2&&token[0]=='"'&&token[1]=='"')
189+
returnpstrdup("");
190+
/* otherwise, we must remove protective backslashes added by outToken */
191+
returndebackslash(token,length);
192+
}
183193

184194

185195
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp