Text function problem
Text function problem
От:
Adriaan Joubert <a.joubert@albourne.com>
Дата:
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'
);
=================================================================================
Re: [GENERAL] Text function problem
От:
tolik@icomm.ru (Anatoly K. Lasareff)
Дата:
>>>>> "AJ" == Adriaan Joubert writes:
AJ> Hi,
AJ> I've been adding some bit functions to postgres and this works fine,
AJ> until I tried to construct a function that returns 8 bits as a string,
AJ> e.g. 4 as 00000100. I constructed the function with the help of the
AJ> programmer manual.
AJ> Now I have the following problem on Friday's snapshot: the first time I
AJ> select an integer as bitstring it works fine, but the second time I get
AJ> a segflt. So I'm evidently missing something somewhere. I'd appreciate
AJ> it if somebody could tell me what I'm doing wrong.
AJ> I'm getting (from the backend)
. . .
skip
. . .
AJ> text * bout (int a) {
AJ> int32 new_text_size = VARHDRSZ + sizeof(char)*8;
AJ> text *new_text = (text *) palloc(new_text_size);
AJ> int i;
AJ> for (i=0; i<8; i++)
AJ> VARDATA(new_text)[i] = (a>>(7-i))%2 ? '1' : '0';
AJ> return new_text;
AJ> }
You do not set length of data in *new_text. You must write about this:
---------------------------------
text * bout (int a) {
int32 new_text_size = VARHDRSZ + sizeof(char)*8;
text *new_text = (text *) palloc(new_text_size);
int i;
VARSIZE(new_text) = new_text_size; /* !!!!! */
for (i=0; i<8; i++)
VARDATA(new_text)[i] = (a>>(7-i))%2 ? '1' : '0';
return new_text;
}
---------------------------------
More: it'll be better if you change 'int' to 'int4' data type in your
nexts.
--
Anatoly K. Lasareff Email: tolik@icomm.ru
Senior programmer