Skip to content

Commit 8a4930e

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 a50b605 commit 8a4930e

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
@@ -1325,33 +1325,32 @@ do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull)
13251325
* Should only be used with a single-TEXT-attribute tupdesc.
13261326
*/
13271327
void
1328-
do_text_output_multiline(TupOutputState *tstate, char *text)
1328+
do_text_output_multiline(TupOutputState *tstate, const char *txt)
13291329
{
13301330
Datum values[1];
13311331
bool isnull[1] = {false};
13321332

1333-
while (*text)
1333+
while (*txt)
13341334
{
1335-
char *eol;
1335+
const char *eol;
13361336
int len;
13371337

1338-
eol = strchr(text, '\n');
1338+
eol = strchr(txt, '\n');
13391339
if (eol)
13401340
{
1341-
len = eol - text;
1342-
1341+
len = eol - txt;
13431342
eol++;
13441343
}
13451344
else
13461345
{
1347-
len = strlen(text);
1348-
eol += len;
1346+
len = strlen(txt);
1347+
eol = txt + len;
13491348
}
13501349

1351-
values[0] = PointerGetDatum(cstring_to_text_with_len(text, len));
1350+
values[0] = PointerGetDatum(cstring_to_text_with_len(txt, len));
13521351
do_tup_output(tstate, values, isnull);
13531352
pfree(DatumGetPointer(values[0]));
1354-
text = eol;
1353+
txt = eol;
13551354
}
13561355
}
13571356

src/include/executor/executor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ typedef struct TupOutputState
284284
extern TupOutputState *begin_tup_output_tupdesc(DestReceiver *dest,
285285
TupleDesc tupdesc);
286286
extern void do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull);
287-
extern void do_text_output_multiline(TupOutputState *tstate, char *text);
287+
extern void do_text_output_multiline(TupOutputState *tstate, const char *txt);
288288
extern void end_tup_output(TupOutputState *tstate);
289289

290290
/*

0 commit comments

Comments
 (0)