Re: C function woes

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: C function woes
Дата
Msg-id 6943.982709543@sss.pgh.pa.us
обсуждение исходный текст
Ответ на C function woes  (Chris Hayner <hayner80@astro.ocis.temple.edu>)
Ответы Re: C function woes
Re: C function woes
Список pgsql-general
Chris Hayner <hayner80@astro.ocis.temple.edu> writes:
> text *
> hello()
>  {
>         char data[] = "hello world";
>         int32 new_text_size = VARHDRSZ + sizeof(data);
>         text *new_text = (text *) palloc(new_text_size);

>         strcpy(VARDATA(new_text), data);
>         return new_text;
>  }


You forgot to set the size word of the new text object.  Also, the
strcpy() looks dangerous since it will copy data[]'s trailing null,
which you do not want and did not allocate room for.  In short:

text *
hello()
 {
        char data[] = "hello world";
        int32 new_text_size = VARHDRSZ + sizeof(data);
        text *new_text = (text *) palloc(new_text_size);

    VARSIZE(new_text) = new_text_size;
        memcpy(VARDATA(new_text), data, sizeof(data));
        return new_text;
 }

            regards, tom lane

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

Предыдущее
От: Chris Hayner
Дата:
Сообщение: C function woes (more info)
Следующее
От: Marko Kreen
Дата:
Сообщение: Re: C function woes (more info)