Re: BUG #2574: C function: arg TEXT data corrupt

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Michael Enke" <michael(dot)enke(at)wincor-nixdorf(dot)com>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #2574: C function: arg TEXT data corrupt
Date: 2006-08-14 15:27:00
Message-ID: 9678.1155569220@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-general

"Michael Enke" <michael(dot)enke(at)wincor-nixdorf(dot)com> writes:
> I created a C function:
> extern Datum test_arg(PG_FUNCTION_ARGS);
> PG_FUNCTION_INFO_V1(test_arg);
> Datum test_arg(PG_FUNCTION_ARGS) {
> elog(INFO, "arg: %s", VARDATA(PG_GETARG_TEXT_P(0)));
> PG_RETURN_INT16(0);

The VARDATA of a TEXT datum is not a C string; in particular it is not
guaranteed to be null-terminated. This is an error in your code not
a bug.

The usual way to get a C string from a TEXT datum is to call textout,
eg

str = DatumGetCString(DirectFunctionCall1(textout, datumval));

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2006-08-14 15:29:32 Re: no native spinlock support on os x 10.4.7
Previous Message Michael Enke 2006-08-14 13:53:34 BUG #2574: C function: arg TEXT data corrupt

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2006-08-14 15:29:48 Re: prepared statement already exists
Previous Message AgentM 2006-08-14 15:14:36 Re: Best approach for a "gap-less" sequence