Thread: Example code bug: destination->data

Example code bug: destination->data

From
PG Doc comments form
Date:
The following documentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/15/xfunc-c.html
Description:

38.10.2. Base Types in C-Language Functions

In the last example of the section, this line appears:

memcpy(destination->data, buffer, 40);

For me, this gave the following error:

error: ‘text’ {aka ‘struct varlena’} has no member named ‘data’

After tracking down the struct definition, I got the example working with:
destination->vl_dat instead.  I assume this is correct, and that the member
name changed somewhere along the way.

--
Ray Brinzer

Re: Example code bug: destination->data

From
Tom Lane
Date:
PG Doc comments form <noreply@postgresql.org> writes:
> 38.10.2. Base Types in C-Language Functions

> In the last example of the section, this line appears:
> memcpy(destination->data, buffer, 40);
> For me, this gave the following error:
> error: ‘text’ {aka ‘struct varlena’} has no member named ‘data’

> After tracking down the struct definition, I got the example working with: 
> destination->vl_dat instead.  I assume this is correct, and that the member
> name changed somewhere along the way.

Hmm.  It's correct by reference to the sample definition of struct
text just above ... but you're right that that has relatively little
to do with the definition we actually use nowadays.

However, referencing vl_dat[] isn't considered good style either
--- AFAICS there are exactly zero direct uses of that name.
The style actually used in the code is to rely on VARDATA() or
VARDATA_ANY(), as in the concat_text() example further down.

Maybe the best fix is to leave the example as it stands but add
a note that this is an oversimplified example?

            regards, tom lane