Skip to content

Commit de52198

Browse files
committed
Fix latent crash in do_text_output_multiline().
do_text_output_multiline() would fail (typically with a null pointer dereference crash) if its input string did not end with a newline. Such cases do not arise in our current sources; but it certainly could happen in future, or in extension code's usage of the function, so we should fix it. To fix, replace "eol += len" with "eol = text + len". While at it, make two cosmetic improvements: mark the input string const, and rename the argument from "text" to "txt" to dodge pgindent strangeness (since "text" is a typedef name). Even though this problem is only latent at present, it seems like a good idea to back-patch the fix, since it's a very simple/safe patch and it's not out of the realm of possibility that we might in future back-patch something that expects sane behavior from do_text_output_multiline(). Per report from Hao Lee. Report: <CAGoxFiFPAGyPAJLcFxTB5cGhTW2yOVBDYeqDugYwV4dEd1L_Ag@mail.gmail.com>
1 parent 7ac0342 commit de52198

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/backend/executor/execTuples.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,33 +1283,32 @@ do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull)
12831283
* Should only be used with a single-TEXT-attribute tupdesc.
12841284
*/
12851285
void
1286-
do_text_output_multiline(TupOutputState *tstate, char *text)
1286+
do_text_output_multiline(TupOutputState *tstate, const char *txt)
12871287
{
12881288
Datum values[1];
12891289
bool isnull[1] = {false};
12901290

1291-
while (*text)
1291+
while (*txt)
12921292
{
1293-
char *eol;
1293+
const char *eol;
12941294
int len;
12951295

1296-
eol = strchr(text, '\n');
1296+
eol = strchr(txt, '\n');
12971297
if (eol)
12981298
{
1299-
len = eol - text;
1300-
1299+
len = eol - txt;
13011300
eol++;
13021301
}
13031302
else
13041303
{
1305-
len = strlen(text);
1306-
eol += len;
1304+
len = strlen(txt);
1305+
eol = txt + len;
13071306
}
13081307

1309-
values[0] = PointerGetDatum(cstring_to_text_with_len(text, len));
1308+
values[0] = PointerGetDatum(cstring_to_text_with_len(txt, len));
13101309
do_tup_output(tstate, values, isnull);
13111310
pfree(DatumGetPointer(values[0]));
1312-
text = eol;
1311+
txt = eol;
13131312
}
13141313
}
13151314

src/include/executor/executor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ typedef struct TupOutputState
276276
extern TupOutputState *begin_tup_output_tupdesc(DestReceiver *dest,
277277
TupleDesc tupdesc);
278278
extern void do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull);
279-
extern void do_text_output_multiline(TupOutputState *tstate, char *text);
279+
extern void do_text_output_multiline(TupOutputState *tstate, const char *txt);
280280
extern void end_tup_output(TupOutputState *tstate);
281281

282282
/*

0 commit comments

Comments
 (0)