Text function problem

From: Adriaan Joubert <a(dot)joubert(at)albourne(dot)com>
To: Postgres-General <pgsql-general(at)postgreSQL(dot)org>
Subject: Text function problem
Date: 1999-06-01 13:46:02
Message-ID: 3753E41A.41F9CFA4@albourne.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I've been adding some bit functions to postgres and this works fine,
until I tried to construct a function that returns 8 bits as a string,
e.g. 4 as 00000100. I constructed the function with the help of the
programmer manual.

Now I have the following problem on Friday's snapshot: the first time I
select an integer as bitstring it works fine, but the second time I get
a segflt. So I'm evidently missing something somewhere. I'd appreciate
it if somebody could tell me what I'm doing wrong.

I'm getting (from the backend)

=================================================================================
darkstar$ postgres tt

POSTGRES backend interactive interface
$Revision: 1.117 $ $Date: 1999/05/26 12:55:55 $

backend> select bout(4);
blank
1: bout (typeid = 25, len = -1, typmod = -1, byval = f)
----
1: bout = "00000100" (typeid = 25, len = -1, typmod = -1,
byval = f)
----
backend> select bout(4);
blank
1: bout (typeid = 25, len = -1, typmod = -1, byval = f)
----
Segmentation fault
=================================================================================

This is on a DEC Alpha running DU 4.0D.

Thanks

Adriaan

Here is the C file:

=================================================================================
#include "bit.h"
#include "postgres.h"

int bor (int a, int b) {
return a|b;
}

int band (int a, int b) {
return a&b;
}

int bxor (int a, int b) {
return a^b;
}

int bnot (int a) {
return ~a;
}

int bshift (int a, int s) {
return s>0 ? a << s : a >> -s;
}

int bbool (int a) {
return a!=0 ? 1 : 0;
}

text * bout (int a) {
int32 new_text_size = VARHDRSZ + sizeof(char)*8;
text *new_text = (text *) palloc(new_text_size);
int i;
for (i=0; i<8; i++)
VARDATA(new_text)[i] = (a>>(7-i))%2 ? '1' : '0';
return new_text;
}
=================================================================================

And here is the SQL

=================================================================================
drop function bor(int4,int4);
create function bor(int4,int4) returns int4
as 'PGLIB/contrib/bit.so'
language 'C';

drop function band(int4,int4);
create function band(int4,int4) returns int4
as 'PGLIB/contrib/bit.so'
language 'C';

drop function bxor(int4,int4);
create function bxor(int4,int4) returns int4
as 'PGLIB/contrib/bit.so'
language 'C';

drop function bnot(int4);
create function bnot(int4) returns int4
as 'PGLIB/contrib/bit.so'
language 'C';

drop function bshift(int4,int4);
create function bshift(int4,int4) returns int4
as 'PGLIB/contrib/bit.so'
language 'C';

drop function bbool(int4);
create function bbool(int4) returns int4
as 'PGLIB/contrib/bit.so'
language 'C';

drop function bout(int4);
create function bout(int4) returns text
as 'PGLIB/contrib/bit.so'
language 'C';

drop aggregate bor_agg int4;
create aggregate bor_agg(
sfunc1 = bor,
basetype = int4,
stype1 = int4,
initcond1 = '0'
);

drop aggregate band_agg int4;
create aggregate band_agg(
sfunc1 = band,
basetype = int4,
stype1 = int4,
initcond1 = '-1'
);
=================================================================================

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Georges Martin 1999-06-01 13:58:50 Array of char(n) ?
Previous Message Michael J Davis 1999-06-01 13:24:30 RE: [GENERAL][SQL] 'denormalising' with a select