Fwd: [GENERAL] returning CHAR from C function - Mailing list pgsql-bugs

From elein
Subject Fwd: [GENERAL] returning CHAR from C function
Date
Msg-id 200212010048.gB10muE9266270@pimout1-ext.prodigy.net
Whole thread Raw
Responses Re: Fwd: [GENERAL] returning CHAR from C function
List pgsql-bugs
Joe Conway confirmed this problem and said that it seems=20
that the function definition is rewritten to return
1042 (=3D=3Dbpchar), instead of  type 18 (=3D=3Dchar).

The workaround is to change the SQL definition to have=20
the function return "char" instead of char (no quotes).

I think it is still a bug, though not high priority since it has
a workaround.

elein@varlena.com

----------  Forwarded Message  ----------

Subject: [GENERAL] returning CHAR from C function
Date: Sat, 30 Nov 2002 14:55:43 -0800
From: elein <elein@sbcglobal.net>
To: pgsql-general@postgresql.org
Cc: elein@varlena.com

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 =3D (text *) PG_GETARG_TEXT_P(0);
        char retdata =3D *(VARDATA(val)) ;
        PG_RETURN_CHAR( retdata );
}

/* Verbatim from utils/adt/char.c; changed name of function only; */
Datum
retchar0(PG_FUNCTION_ARGS)
{
        text       *arg1 =3D 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 =3D *(VARDATA(arg1));
        else
                result =3D '\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.

pgsql-bugs by date:

Previous
From: Justin Clift
Date:
Subject: Re: Pg_dump uses up RAM and swap space
Next
From: Tom Lane
Date:
Subject: Re: Fwd: [GENERAL] returning CHAR from C function