Re: Weird problems with C extension and bytea as input type

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Adrian Schreyer <ams214(at)cam(dot)ac(dot)uk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Weird problems with C extension and bytea as input type
Date: 2011-03-22 21:14:47
Message-ID: AANLkTikTPnsnUybaBR0dL16m9NLn_nA=QB+XMyd2nx4T@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Mar 22, 2011 at 11:57 AM, Adrian Schreyer <ams214(at)cam(dot)ac(dot)uk> wrote:
> On Tue, Mar 22, 2011 at 16:07, Merlin Moncure <mmoncure(at)gmail(dot)com> wrote:
>> On Tue, Mar 22, 2011 at 8:22 AM, Adrian Schreyer <ams214(at)cam(dot)ac(dot)uk> wrote:
>>> Hi,
>>>
>>> I have a weird problem with my custom functions (written in C,C++)
>>> that use bytea as input type (cstring works fine). The functions will
>>> work as expected if they are the only function that uses the bytea
>>> column in a query; as soon as there is a second function using the
>>> same column, the C function will return one of the following: an empty
>>> cstring, a substring of the bytea or the correct cstring. Based on
>>> these symptoms I assume there is something fundamental that I do wrong
>>> (or that is missing) with handling the bytea pointer.
>>>
>>> In one specific example, the bytea contains a binary file format that
>>> the function converts into a string format. I convert the bytea to a
>>> C++ string with string(VARDATA(b), VARSIZE(b)-VARHDRSZ).
>>>
>>> bytea *b = PG_GETARG_BYTEA_P(0);
>>> char *ism;
>>>
>>> ism = function(b);
>>>
>>> PG_RETURN_CSTRING(ism);
>>
>> your problem is probably inside 'function' -- are you properly copying
>> the data out of the bytea struct?.  also, are you really sure you want
>> to be returning cstring type, not text?
>>
>> merlin
>>
>
> yes, the function in the C++ toolkit I use takes a std::string as
> input. So far I have used string(VARDATA(oeb), VARSIZE(oeb)-VARHDRSZ)
> to convert it. I want to change both input and output to text later on
> once this is working. I suspect I will get similar problems with
> pg_getarg_text_p() or any other struct. I think you are right, I am
> not coping the data out of the struct correctly; all my functions that
> use VARDATA() have these problems. What would be the proper way?

Well, C++ string constructor is proper in the sense it makes copy of
the source data. however, it's a little weird that you are passing
bytea like this...bytea can contain null and c++ string initialization
stops at any 0 byte. Maybe you should be encoding the data to text
(say, to hex) first?

merlin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Raymond O'Donnell 2011-03-22 21:16:40 Re: postgres conferences missing videos?
Previous Message Thomas Kellerer 2011-03-22 20:56:45 Re: Install issues