more custom C function fun

From: "Dan \"Heron\" Myers" <heron(at)xnapid(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: more custom C function fun
Date: 2008-05-06 05:43:40
Message-ID: 481FF00C.4060408@xnapid.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have a custom C function that takes two text*s and returns a text*.

My problem is with this code:

PG_FUNCTION_INFO_V1(get_agent);

PGMODULEEXPORT Datum get_agent(PG_FUNCTION_ARGS)
{
if(!PG_ARGISNULL(0))
{
text* calling_party = PG_GETARG_TEXT_P(0);

char* thestr = VARDATA(calling_party);
if(thestr[20] == ')')
{
PG_RETURN_TEXT_P(calling_party);
}
}
/* the other argument is ignored for now */
PG_RETURN_NULL();
}

The problem is, the comparison in the inner if statement is always true.
If I change to compare, say, thestr[0] == 'N', then it works as
expected (returning only those text*s whose first letter is N, returning
null for the rest).

However if I try to compare any character inside the text* with a
parenthesis (both '(' and ')'), then the equality is apparently always
true (the function never returns null, always returning calling_party),
whether or not there is any data in that column that contains a
parenthesis in that column.

Does anyone know of any oddities or whatnot I should be accounting for
when reading character data out of a text*?

- Dan

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dan "Heron" Myers 2008-05-06 06:05:18 Re: more custom C function fun
Previous Message Craig Ringer 2008-05-06 04:02:28 Re: Custom C function - is palloc broken?