Re: Custom data type in C with one fixed and one variable attribute - Mailing list pgsql-general

From Tom Lane
Subject Re: Custom data type in C with one fixed and one variable attribute
Date
Msg-id 7306.1319754201@sss.pgh.pa.us
Whole thread Raw
In response to Custom data type in C with one fixed and one variable attribute  (Adrian Schreyer <ams214@cam.ac.uk>)
Responses Re: Custom data type in C with one fixed and one variable attribute  (Adrian Schreyer <ams214@cam.ac.uk>)
List pgsql-general
Adrian Schreyer <ams214@cam.ac.uk> writes:
> The data type I have is

> typedef struct {
>         int4   length;
>         uint32 foo;
>         char   bar[1];
>     } oefp;

Seems reasonable enough.

>     mydatatype *dt = (mydatatype*) palloc(VARHDRSZ + sizeof(uint32) +
> strlen(buffer));

>     SET_VARSIZE(dt, VARHDRSZ + sizeof(uint32) + strlen(buffer));
>     memcpy(dt->bar, buffer, strlen(buffer));
>     dt->foo = foo;

Fine, but keep in mind that what you are creating here is a
non-null-terminated string.

> The problem is however that dt->bar contains not only the input string
> but random characters or other garbage as well, so something must go
> wrong at the end of the function. Any thoughts what it could be?

It sounds to me like you are inspecting dt->bar with something that
expects to see a null-terminated string.  You could either fix your
inspection code, or expend one more byte to make the string be
null-terminated as stored.

            regards, tom lane

pgsql-general by date:

Previous
From: Brian Fehrle
Date:
Subject: Re: Server hitting 100% CPU usage, system comes to a crawl.
Next
From: Alan Hodgson
Date:
Subject: Re: Server hitting 100% CPU usage, system comes to a crawl.