Thread: Bug #894: different result with the same parameterlist in function call
Bug #894: different result with the same parameterlist in function call
From
pgsql-bugs@postgresql.org
Date:
Kovacs Balazs (balazs.kovacs@jonapot.hu) reports a bug with a severity of 3 The lower the number the more severe it is. Short Description different result with the same parameterlist in function call Long Description I have written a simple function in C under pg (it uses regex lib): #include <stdio.h> #include <string.h> #include <regex.h> #include <postgres.h> #include <fmgr.h> PG_FUNCTION_INFO_V1(crightevaledge); Datum crightevaledge(PG_FUNCTION_ARGS) { text *pattern = (text*)PG_GETARG_TEXT_P(0); text *sample = (text*)PG_GETARG_TEXT_P(1); char *char_pattern = (char*)VARDATA(pattern); char *char_sample = (char*)VARDATA(sample); regex_t compre_pattern; int32 retval; if (regcomp(&compre_pattern,char_pattern,0)!=0) PG_RETURN_INT32(2); if (regexec(&compre_pattern,char_sample,0,NULL,0)==0) retval=1; else retval=0; regfree(&compre_pattern); PG_RETURN_INT32(retval); } I compile it: gcc -fpic -c -I/usr/local/postgresql-7.2.1/src/include -I/usr/local/pgsql/include sample.c gcc -shared -o sample.so sample.o and create a function under pg: create function crightevaledge(text,text) returns int4 as '/devel/pg/sample.so' language 'C'; I have created a simple table for test, like this (in an empty database (named test)): create table test(descr text, path text); I put into it some records: insert into test values('test1','test1.dat'); insert into test values('test2','test2.mpg'); insert into test values('test3','test3.pdf'); After, i executed the following query more than 1 times: select descr,crightevaledge('.*mpg',path) from test; The result was different on different executing: test=# select descr,crightevaledge('.*mpg',path) from test; descr | crightevaledge -------+---------------- test1 | 0 test2 | 0 test3 | 0 (3 rows) test=# select descr,crightevaledge('.*mpg',path) from test; descr | crightevaledge -------+---------------- test1 | 0 test2 | 1 test3 | 0 (3 rows) test=# select descr,crightevaledge('.*mpg',path) from test; descr | crightevaledge -------+---------------- test1 | 0 test2 | 0 test3 | 0 (3 rows) test=# select descr,crightevaledge('.*mpg',path) from test; descr | crightevaledge -------+---------------- test1 | 0 test2 | 1 test3 | 0 (3 rows) I use postgres 7.2.1 version, and redhat 7.2 (with kernel 2.4.20). Sample Code No file was uploaded with this report
pgsql-bugs@postgresql.org writes: > text *pattern = (text*)PG_GETARG_TEXT_P(0); > text *sample = (text*)PG_GETARG_TEXT_P(1); > char *char_pattern = (char*)VARDATA(pattern); > char *char_sample = (char*)VARDATA(sample); You can't convert text* to char* with just a cast. The result won't (usually) be null-terminated where it should be; so you end up with strings that have garbage on the end. Use textout() like all the existing code does when it wants a null-terminated string. regards, tom lane