Re: It's seems that the function "do_text_output_multiline" does not suit for format "line1\nline2\n...lineN".

From: David Rowley <david(dot)rowley(at)2ndquadrant(dot)com>
To: Hao Lee <mixtrue(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: It's seems that the function "do_text_output_multiline" does not suit for format "line1\nline2\n...lineN".
Date: 2016-05-20 12:12:24
Message-ID: CAKJS1f9v5vTC0fbynvutKO2HPpMJWQPhWAyDMHxJo1cTT=t=Tw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 20 May 2016 at 19:13, Hao Lee <mixtrue(at)gmail(dot)com> wrote:
>
> Hi all,
>
> Today, I am do some works on adding some customized featues to PostgreSQL 9.6 beta1. But, when i do some output to psql using the fuction "do_text_output_multiline" with the string just like mentioned in mail tilte, such as "this is a\ntest for\nnew blank.". the PostgreSQL may lead to corruption in this function, and i debugged it that found this function can not dealt with the boundaries properly. The original function code as :
>
> do_text_output_multiline(TupOutputState *tstate, char *text)
> {
> Datum values[1];
> bool isnull[1] = {false};
>
> while (*text)
> {
> char *eol;
> int len;
>
> eol = strchr(text, '\n');
> if (eol)
> {
> len = eol - text;
>
> eol++;
> }
> else
> {
> len = strlen(text);
> eol += len;
> }
>
> values[0] = PointerGetDatum(cstring_to_text_with_len(text, len));
> do_tup_output(tstate, values, isnull);
> pfree(DatumGetPointer(values[0]));
> text = eol;
> }
> }
>

Thanks for reporting this. It does seem pretty broken. I guess we've
only gotten away with this due to EXPLAIN output lines always having a
\n at the end of them, but we should fix this.

Your proposed fix looks a little bit confused. You could have just
removed the eol += len; as testing if (eol) in the else will never be
true as that else is only being hit because eol is NULL.

I shuffled things around in there a bit and came up with the attached fix.

--
David Rowley http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

Attachment Content-Type Size
do_text_output_multiline_fix.patch application/octet-stream 745 bytes

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jesper Pedersen 2016-05-20 12:48:46 Re: Speedup twophase transactions
Previous Message Amit Langote 2016-05-20 08:37:23 Re: Declarative partitioning