pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: mengfanjun <meng_fan_jun@foxmail.com>
To: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Call pstrdup() of palloc.h will change source string, please help!
Date: Sat, 4 Jun 2022 11:07:56 +0800
Message-ID: <tencent_755B695BF291AADC410AF2D15D2114B36206@qq.com> (raw)

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?

view thread (2+ messages)  latest in thread

Message-ID: <tencent_755B695BF291AADC410AF2D15D2114B36206@qq.com>
Permalink:  ../tencent_755B695BF291AADC410AF2D15D2114B36206@qq.com/
Also on:    postgresql.org/message-id/tencent_755B695BF291AADC410AF2D15D2114B36206@qq.com

 · 

reply

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-general@postgresql.org
  Cc: meng_fan_jun@foxmail.com
  Subject: Re: Call pstrdup() of palloc.h will change source string, please help!
  In-Reply-To: <tencent_755B695BF291AADC410AF2D15D2114B36206@qq.com>
  References: <tencent_755B695BF291AADC410AF2D15D2114B36206@qq.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