pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Call pstrdup() of palloc.h will change source string, please help!
2+ messages / 2 participants
[nested] [flat]

* Call pstrdup() of palloc.h will change source string, please help!
@ 2022-06-04 03:07 mengfanjun <meng_fan_jun@foxmail.com>
  2022-06-04 17:05 ` Re: Call pstrdup() of palloc.h will change source string, please help! Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: mengfanjun @ 2022-06-04 03:07 UTC (permalink / raw)
  To: pgsql-general <pgsql-general@lists.postgresql.org>

Hello,


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


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

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

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

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

        *results[i] = token;
    }
    
    return isSuccess;
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


pstrdup() will change source "system_time( stt , ett )" to "system_time( stt " and return "system_time( stt " when it is called in 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?

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Call pstrdup() of palloc.h will change source string, please help!
  2022-06-04 03:07 Call pstrdup() of palloc.h will change source string, please help! mengfanjun <meng_fan_jun@foxmail.com>
@ 2022-06-04 17:05 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2022-06-04 17:05 UTC (permalink / raw)
  To: mengfanjun <meng_fan_jun@foxmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

"=?ISO-8859-1?B?bWVuZ2Zhbmp1bg==?=" <meng_fan_jun@foxmail.com> writes:
> pstrdup() will change source "system_time( stt , ett )" to "system_time( stt " and return "system_time( stt " when it is called in ValidateForeignTableOptions() and CStoreGetOptions().

That claim is not very credible.  I'm not sure where the bug is in this
code, but the chance that it's in pstrdup is frankly zero.

According to the man page for strsep(), that function modifies the string
pointed to by its first argument.  I suspect you're failing to account
for that.  I'm also suspicious of the "// trim" code, which will misbehave
for a zero-length token.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2022-06-04 17:05 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-06-04 03:07 Call pstrdup() of palloc.h will change source string, please help! mengfanjun <meng_fan_jun@foxmail.com>
2022-06-04 17:05 ` Tom Lane <tgl@sss.pgh.pa.us>

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