Text function problem - Mailing list pgsql-general

From Adriaan Joubert
Subject Text function problem
Date
Msg-id 3753E41A.41F9CFA4@albourne.com
Whole thread Raw
List 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'
);
=================================================================================

pgsql-general by date:

Previous
From: Michael J Davis
Date:
Subject: RE: [GENERAL][SQL] 'denormalising' with a select
Next
From: Georges Martin
Date:
Subject: Array of char(n) ?