Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8999f5e

Browse files
committed
Fix write/read of empty string fields in Nodes.
Historically, outToken has represented both NULL and empty-string strings as "<>", which readfuncs.c then read as NULL, thus failing to preserve empty-string fields accurately. Remarkably, this has not 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 true already, apparently as a hangover from an ancient time when string quoting 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 parent af51b2f commit 8999f5e

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/backend/nodes/outfuncs.c

+18-3
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(StringInfo str, const char *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
char in[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

+12-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,18 @@
178178

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

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

184194

185195
/*

0 commit comments

Comments
 (0)