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