Re: C Extension woes

Поиск
Список
Период
Сортировка
От Jan Urbański
Тема Re: C Extension woes
Дата
Msg-id 48A30955.8000500@students.mimuw.edu.pl
обсуждение исходный текст
Ответ на Re: C Extension woes  (Tim Hawes <thawes@novadine.com>)
Ответы Re: C Extension woes  (Tim Hawes <thawes@novadine.com>)
Список pgsql-hackers
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);
}

-- 
Jan Urbanski
GPG key ID: E583D7D2

ouden estin


В списке pgsql-hackers по дате отправления:

Предыдущее
От: Simon Riggs
Дата:
Сообщение: Re: Transaction-controlled robustness for replication
Следующее
От: Tim Hawes
Дата:
Сообщение: Re: C Extension woes