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

From: David W Noon <dwnoon(at)ntlworld(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Weird problems with C extension and bytea as input type
Date: 2011-03-22 22:21:55
Message-ID: 20110322222155.30d8cab6@karnak.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 22 Mar 2011 16:14:47 -0500, Merlin Moncure wrote about Re:
[GENERAL] Weird problems with C extension and bytea as input type:

[snip]
>>> On Tue, Mar 22, 2011 at 8:22 AM, Adrian Schreyer <ams214(at)cam(dot)ac(dot)uk>
>>> wrote:
[snip]
>>>> bytea *b = PG_GETARG_BYTEA_P(0);
>>>> char *ism;
>>>>
>>>> ism = function(b);
>>>>
>>>> PG_RETURN_CSTRING(ism);

What is the prototype for function()? If it returns a char * then you
will likely have either scope problems, reentrancy problems or memory
leaks. If you are going to buy the C++ religion then you usually need to
buy it wholesale: do all if your string processing as std::string
objects and only return to char * when you revert to C.

As a rough example:

bytea *b = PG_GETARG_BYTEA_P(0);
std::string ism;

ism = function(std::string(VARDATA(b), VARSIZE(b)-VARHDRSZ));

PG_RETURN_CSTRING(ism.c_str());

Note that this returns an ASCIIZ string, which is not necessarily the
same as the C++ string. You would be better off creating a
PostgreSQL text object and then return that.

>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.

Not so. If the constructor also specifies a length then the data
pointer's area is not assumed to be NUL-terminated.

>Maybe you should be encoding the data to text (say, to hex) first?

Better to use the supplied length in the varlena descriptor.
--
Regards,

Dave [RLU #314465]
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
dwnoon(at)ntlworld(dot)com (David W Noon)
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Ribe 2011-03-22 22:28:27 Re: Trigger Function return values
Previous Message Andy Chambers 2011-03-22 22:12:56 Trigger Function return values