SuSE 7.3 Postgres7.3b5
I hope this is a simple user error. I am trying to
return the first character of a text type in a C function.
I did the obvious thing and it crashed the server.
I stole text_char out of utils/adt/char.c and it crashed
the server.
I suspect I have some incorrect expectations of PG_RETURN_CHAR()
or PG_RETURN_CHAR() maybe should call CharGetDatum, not DatumGetChar().
Or??
fmgr.h:#define PG_GETARG_CHAR(n) DatumGetChar(PG_GETARG_DATUM(n))
postgres.h:#define DatumGetChar(X) ((char) GET_1_BYTE(X))
postgres.h:#define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
postgres.h:#define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
Code follows...
Thanks,
Elein
----- retchar.c ---------------
/*
* FUNCTION: input text/cstring, return char.
#
*/
#include "postgres.h"
#include "fmgr.h"
PG_FUNCTION_INFO_V1(retchar);
PG_FUNCTION_INFO_V1(retchar1);
/*
* Fetch first character of text.
* Returns char
*/
Datum
retchar( PG_FUNCTION_ARGS )
{
text *val = (text *) PG_GETARG_TEXT_P(0);
char retdata = *(VARDATA(val)) ;
PG_RETURN_CHAR( retdata );
}
/* Verbatim from utils/adt/char.c; changed name of function only; */
Datum
retchar0(PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_P(0);
char result;
/*
* An empty input string is converted to \0 (for consistency with
* charin). If the input is longer than one character, the excess data
* is silently discarded.
*/
if (VARSIZE(arg1) > VARHDRSZ)
result = *(VARDATA(arg1));
else
result = '\0';
PG_RETURN_CHAR(result);
}
----- retchar.sql ---------------
--
-- retchar function definitions
--
drop function retchar(text);
create function retchar(text)
returns char
as '$libdir/retchar.so'
language 'c';
drop function retchar0(text);
create function retchar0(text)
returns char
as '$libdir/retchar.so'
language 'c';
---------- retchar_test.sql -------------
\echo both selects crash server
select retchar('abc');
select retchar1('abc');
--
----------------------------------------------------------------------------------------
elein@varlena.com Database Consulting www.varlena.com
I have always depended on the [QA] of strangers.