C function and NULL result

From: Victor Ivanov <v0rbiz(at)icon(dot)bg>
To: pgsql-general(at)postgresql(dot)org
Subject: C function and NULL result
Date: 2000-10-17 10:27:13
Message-ID: 20001017132713.B19114@icon.icon-bg.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

Sorry for the long message.

I'm using PostgreSQL 7.0.2 and I'm trying to create my own types with
input and output functions in C. Everything works fine, but when the input
is invalid and the function returns NULL, the backend crashes with SIGSEGV.

test.c:

#include <stdio.h>
#include <postgres.h>

typedef struct {
int x, y;
} testtype;

testtype *testtype_in(char *str) {
int x, y;
char *ep;
testtype *ret = NULL;

x = strtol(str, &ep, 10);
if (ep && *ep == ' ') {
y = strtol(ep, &ep, 10);
if (ep && (*ep == ' ' || *ep == 0)) {
ret = (testtype *)palloc(sizeof(testtype));
ret->x = x;
ret->y = y;
}
}

return ret;
}

char *testtype_out(testtype *tt) {
char *ret;

if (tt == NULL) return NULL;
ret = (char *)palloc(32);
snprintf(ret, 32, "%i %i", tt->x, tt->y);

return ret;
}

compiled it with cc -o test.so -fPIC -shared test.c -I/usr/local/pgsql/include
(no errors)

from psql (logged as pgsql superuser):
CREATE FUNCTION testtype_in(opaque)
RETURNS testtype
AS '/usr/local/pgsql/test.so'
LANGUAGE 'c';

CREATE FUNCTION testtype_out(opaque)
RETURNS opaque
AS '/usr/local/pgsql/test.so'
LANGUAGE 'c';

CREATE TYPE testtype (
internallength = 8,
input = testtype_in,
output = testtype_out);

(everything is ok)
CREATE TABLE test1 (test testtype);
(ok)
INSERT INTO test1 (test) values('4 5');
(ok)
SELECT * FROM test1;
returns one row, '4 5'

then I tried with invalid input:
INSERT INTO test1 (test) values('6');
pqReadData() -- backend closed the channel unexpectedly.
...
from the error log:
Server process (pid 25624) exited with status 11 at Tue Oct 17 13:17:58 2000

Actualy I'll access these tables in 'binary mode' so conversion functions
aren't needed... So I will make them return 'NULL' on error... Or is this
the right thing to do on error (according to the documentation it is not)?

Any help will be appreciated

--
Players win and Winners play
Have a lucky day

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Gilles DAROLD 2000-10-17 10:52:58 Re: PL/Perl compilation error
Previous Message Alfred Perlstein 2000-10-17 08:28:04 Re: Limit on number of queries from CGI or PHP (security)