Re: Custom data type in C with one fixed and one variable attribute

From: Adrian Schreyer <ams214(at)cam(dot)ac(dot)uk>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Custom data type in C with one fixed and one variable attribute
Date: 2011-10-28 08:50:14
Message-ID: CACYduyLShsaq44YD2j+PFKvdCh6++1y2gMV_cpmLBjA96LEUhw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I added one more byte to include \0 and its working as expected now.
Thanks for your help!

Cheers,

Adrian

On Thu, Oct 27, 2011 at 23:23, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Adrian Schreyer <ams214(at)cam(dot)ac(dot)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
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2011-10-28 08:52:15 Re: "Named" column default expression
Previous Message Mohamed Hashim 2011-10-28 08:21:22 Re: Performance Problem with postgresql 9.03, 8GB RAM,Quadcore Processor Server--Need help!!!!!!!