dbTalk Databases Forums  

[BUGS] BUG #2574: C function: arg TEXT data corrupt

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] BUG #2574: C function: arg TEXT data corrupt in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael Enke
 
Posts: n/a

Default [BUGS] BUG #2574: C function: arg TEXT data corrupt - 08-14-2006 , 10:00 AM







The following bug has been logged online:

Bug reference: 2574
Logged by: Michael Enke
Email address: michael.enke (AT) wincor-nixdorf (DOT) com
PostgreSQL version: 8.1.4
Operating system: Open Suse 10.1 and CentOS 4.3
Description: C function: arg TEXT data corrupt
Details:

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);
}
and used the
CREATE FUNCTION test_arg(TEXT) RETURNS INT4 AS 'path_to_lib.so' LANGUAGE
'C';

If I call this function "the first time"
after connecting with psql, the info output shows
corrupted data, the second and further call is ok:
me@noteme:~/uwx9/SVN/tpl/trunk/db/postgresql/src> psql -U tplinux
Welcome to psql 8.1.3, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

tplinux=> select test_arg('1');
INFO: arg: 1s@3
test_arg
----------
0
(1 row)

tplinux=> select test_arg('1');
INFO: arg: 1
test_arg
----------
0
(1 row)

The same problem is with input length of 2, 3, 4, 8 char (and may be more).
With input of 4 char I never get correct value.

No problem for first call with 5, 6 , 7 char,
but with 5 char problem if before called with 6 char.

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] BUG #2574: C function: arg TEXT data corrupt - 08-14-2006 , 10:30 AM






"Michael Enke" <michael.enke (AT) wincor-nixdorf (DOT) com> writes:
Quote:
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

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.