Skip site navigation (1) Skip section navigation (2)

Fwd: [GENERAL] returning CHAR from C function

From: elein <elein(at)sbcglobal(dot)net>
To: pgsql-bugs(at)postgresql(dot)org
Cc: elein(at)varlena(dot)com
Subject: Fwd: [GENERAL] returning CHAR from C function
Date: 2002-12-01 00:46:28
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-bugs
Joe Conway confirmed this problem and said that it seems 
that the function definition is rewritten to return
1042 (==bpchar), instead of  type 18 (==char).

The workaround is to change the SQL definition to have 
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.


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

Subject: [GENERAL] returning CHAR from C function
Date: Sat, 30 Nov 2002 14:55:43 -0800
From: elein <elein(at)sbcglobal(dot)net>
To: pgsql-general(at)postgresql(dot)org
Cc: elein(at)varlena(dot)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().

    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...


----- retchar.c ---------------
 * FUNCTION: input text/cstring, return char.
#include "postgres.h"
#include "fmgr.h"


 * Fetch first character of text.
 * Returns char
        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; */
        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));
                result = '\0';

----- retchar.sql ---------------

-- retchar function definitions
drop function retchar(text);
create function retchar(text)
returns char
as '$libdir/'
language 'c';

drop function retchar0(text);
create function retchar0(text)
returns char
as '$libdir/'
language 'c';

---------- retchar_test.sql -------------

\echo both selects crash server
select retchar('abc');
select retchar1('abc');

elein(at)varlena(dot)com     Database Consulting
              I have always depended on the [QA] of strangers.


pgsql-bugs by date

Next:From: Tom LaneDate: 2002-12-01 04:44:59
Subject: Re: Fwd: [GENERAL] returning CHAR from C function
Previous:From: Justin CliftDate: 2002-11-28 23:38:12
Subject: Re: Pg_dump uses up RAM and swap space

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group