8000 Fix latent crash in do_text_output_multiline(). · eldilibra/postgres@a0cc89a · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit a0cc89a

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 9561f6e commit a0cc89a

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
@@ -1285,33 +1285,32 @@ do_tup_output(TupOutputState *tstate, Datum *values, bool *isnull)
12851285
* Should only be used with a single-TEXT-attribute tupdesc.
12861286
*/
12871287
void
1288-
do_text_output_multiline(TupOutputState *tstate, char *text)
1288+
do_text_output_multiline(TupOutputState *tstate, const char *txt)
12891289
{
12901290
Datum values[1];
12911291
bool isnull[1] = {false};
12921292

1293-
while (*text)
1293+
while (*txt)
12941294
{
1295-
char *eol;
1295+
const char *eol;
12961296
int len;
12971297

1298-
eol = strchr(text, '\n');
1298+
eol = strchr(txt, '\n');
12991299
if (eol)
13001300
{
1301-
len = eol - text;
1302-
1301+
len = eol - txt;
13031302
eol++;
13041303
}
13051304
else
13061305
{
1307-
len = strlen(text);
1308-
eol += len;
1306+
len = strlen(txt);
1307+
eol = txt + len;
13091308
}
13101309

1311-
values[0] = PointerGetDatum(cstring_to_text_with_len(text, len));
1310+
values[0] = PointerGetDatum(cstring_to_text_with_len(txt, len));
13121311
do_tup_output(tstate, values, isnull);
13131312
pfree(DatumGetPointer(values[0]));
1314-
text = eol;
1313+
txt = eol;
13151314
}
13161315
}
13171316

src/include/executor/executor.h

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

284284
/*

0 commit comments

Comments
 (0)
0