Call pstrdup() of palloc.h will change source string, please help!

From: mengfanjun <meng_fan_jun(at)foxmail(dot)com>
To: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Call pstrdup() of palloc.h will change source string, please help!
Date: 2022-06-04 03:07:56
Message-ID: tencent_755B695BF291AADC410AF2D15D2114B36206@qq.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

I'm working on modifying an postgresql extension called "cstore_fdw". My function is like:

&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;
static bool
ParsePeriod(const char *periodString, Period *result)
{
&nbsp; &nbsp; bool isSuccess = true;
&nbsp; &nbsp; char *dupPeriodForString;
&nbsp; &nbsp;
&nbsp; &nbsp; char delimiters[3][2] = { "(", ",", ")" };
&nbsp; &nbsp; char **results[3] = {&amp;(result-&gt;periodName), &amp;(result-&gt;start_column_name), &amp;(result-&gt;end_column_name)};
&nbsp; &nbsp;
dupPeriodForString = pstrdup(periodString); // !!!! pstrdup error !!!!

&nbsp; &nbsp; if (dupPeriodForString == NULL || strlen(dupPeriodForString) <= 0 || result == NULL)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; isSuccess = false;
&nbsp; &nbsp; }
&nbsp; &nbsp;

&nbsp; &nbsp; for (int i = 0; i < 3; ++i)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; char *token = NULL;
&nbsp; &nbsp; &nbsp; &nbsp; Size len = 0;
&nbsp; &nbsp; &nbsp; &nbsp; int j;
&nbsp; &nbsp; &nbsp; &nbsp; if ( (token = strsep(&amp;dupPeriodForString, delimiters[i])) == NULL )
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isSuccess = false;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; // trim
&nbsp; &nbsp; &nbsp; &nbsp; len = strlen(token);
&nbsp; &nbsp; &nbsp; &nbsp; while(isspace(token[len - 1])) --len;
&nbsp; &nbsp; &nbsp; &nbsp; while(*token &amp;&amp; isspace(*token)) ++token, --len;
&nbsp; &nbsp; &nbsp; &nbsp; token = pnstrdup(token, len);

&nbsp; &nbsp; &nbsp; &nbsp; // lowercase
&nbsp; &nbsp; &nbsp; &nbsp; len = strlen(token);
&nbsp; &nbsp; &nbsp; &nbsp; for (j = 0; j < len; j++)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; token[j] = tolower(token[j]);
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; *results[i] = token;
&nbsp; &nbsp; }
&nbsp; &nbsp;
&nbsp; &nbsp; return isSuccess;
}
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;

pstrdup() will change source "system_time( stt , ett )" to "system_time( stt " and return&nbsp;"system_time( stt "&nbsp;when it is called in&nbsp;ValidateForeignTableOptions() and CStoreGetOptions(). But if I call it in somewhere else, it won't get wrong.

Here's my enviornment:
postgresql: psql (PostgreSQL) 12.10 (Ubuntu 12.10-0ubuntu0.20.04.1)
compiler: clang version 10.0.0-4ubuntu1
OS: WINDOWS sub linux GNU/Linux 5.10.102.1-microsoft-standard-WSL2 x86_64

How can I do to fix this problem?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bryn Llewellyn 2022-06-04 04:45:52 Re: '{"x": 42, "y": null}'::jsonb != '{"x": 42}'::jsonb ... Really?
Previous Message David G. Johnston 2022-06-04 02:01:11 Re: '{"x": 42, "y": null}'::jsonb != '{"x": 42}'::jsonb ... Really?