From: Hao Lee <mixtrue@gmail.com>
To: pgsql-hackers@postgresql.org
Subject: It's seems that the function "do_text_output_multiline" does not suit for format "line1\nline2\n...lineN".
Date: Fri, 20 May 2016 15:13:03 +0800
Message-ID: <CAGoxFiFPAGyPAJLcFxTB5cGhTW2yOVBDYeqDugYwV4dEd1L_Ag@mail.gmail.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>
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;
}
}
using the string "this is a*\n*test for*\n*new blank." as the second input
parameter, when dealing with the the last part of stirng, "new blank.", the
code "eol = strchr(text, '\n');" will return NULL and will go into the
"else" part, and the varialbe "eol" will added an integer, which is the
length of "new blank.", eol will be 0xa, text is set to 0xa too. this
address will is a system used address, then corruption. I think some
boundary check will be added as following.
do_text_output_multiline(TupOutputState *tstate, char *text)
{
Datum values[1];
bool isnull[1] = {false};
while (*text *&& *text) //*if the text is NULL the return, do nothing.*
{
char *eol;
int len;
eol = strchr(text, '\n');
if (eol)
{
len = eol - text;
eol++;
}
else
{
len = strlen(text);
* if (eol) //to check whether the eol is NULL or not.*
eol += len;
}
values[0] = PointerGetDatum(cstring_to_text_with_len(text, len));
do_tup_output(tstate, values, isnull);
pfree(DatumGetPointer(values[0]));
text = eol;
}
}
Best Regards.
RingsC.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: mixtrue@gmail.com
Subject: Re: It's seems that the function "do_text_output_multiline" does not suit for format "line1\nline2\n...lineN".
In-Reply-To: <CAGoxFiFPAGyPAJLcFxTB5cGhTW2yOVBDYeqDugYwV4dEd1L_Ag@mail.gmail.com>
References: <CAGoxFiFPAGyPAJLcFxTB5cGhTW2yOVBDYeqDugYwV4dEd1L_Ag@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox