Re: C Extension woes

From: Tim Hawes <thawes(at)novadine(dot)com>
To: Jan Urbański <j(dot)urbanski(at)students(dot)mimuw(dot)edu(dot)pl>
Cc: Postgres - Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: C Extension woes
Date: 2008-08-13 16:35:36
Message-ID: 48A30D58.6080408@novadine.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Ok, that worked!
Thank you very much, Jan and others who gave their input.
I did see Tom's input for the VARHDRSZ and tried that, but forgot to add
that again
when I called VARATT_SIZEP

Jan Urbański wrote:
> Tim Hawes wrote:
>
>> @Jan:
>> It appears the cstring_to_text function is unique to the latest
>> PostgreSQL code. I do not have a def for that for PostgreSQL 8.2, and
>
> Oh, I'm sorry, I forgot about that. cstring_to_text has been added
> only recently (it's not even it 8.3, silly me).
>
>> Datum pl_masterkey(PG_FUNCTION_ARGS)
>> {
>> char *e_var = getenv("PGMASTERKEY");
>> size_t length = e_var != NULL ? strlen(e_var) : 0;
>>
>> text * mkey = (text *) palloc(length);
>> VARATT_SIZEP(mkey) = length;
>> memcpy(VARDATA(mkey), e_var, length);
>>
>> PG_RETURN_TEXT_P(mkey);
>> }
>
> You forgot to palloc space for the varlena header. You need an extra
> VARHDRSZ bytes for the header, in addition to what is needed for your
> data.
> Try something like this:
>
> Datum
> pl_masterkey(PG_FUNCTION_ARGS)
> {
> char *e_var;
> size_t length;
> text *mkey;
>
> e_var = getenv("PGMASTERKEY");
> length = e_var ? strlen(e_var) : 0;
> mkey = (text *) palloc(VARHDRSZ + length);
>
> VARATT_SIZEP(mkey) = VARHDRSZ + length;
> memcpy(VARDATA(mkey), e_var, length);
>
> PG_RETURN_TEXT_P(mkey);
> }
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Simon Riggs 2008-08-13 16:46:18 Re: Transaction-controlled robustness for replication
Previous Message Simon Riggs 2008-08-13 16:21:13 Re: Transaction-controlled robustness for replication