Segfault using heap_form_tuple - Mailing list pgsql-hackers

From Claudio Rossi
Subject Segfault using heap_form_tuple
Date
Msg-id JZ2IQE$322CDC8D53003B9CD19D97C281282182@libero.it
Whole thread Raw
Responses Re: Segfault using heap_form_tuple  ("Pavan Deolasee" <pavan.deolasee@gmail.com>)
Re: Segfault using heap_form_tuple  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Greetings,
I'm having a problem using heap_form_tuple. I'm trying to form a HeapTuple to use it later with simple_heap_insert. My
codelooks like this: 

------------------

Datum *values;
bool *nulls;
int natts;
TupleDesc tupDesc;
HeapTuple tuple;
...
tupDesc = RelationGetDescr(...my previously opened relation...);
natts = tupDesc->natts;

values = (Datum *)palloc(natts*sizeof(Datum *));
nulls = (bool *)palloc(natts*sizeof(bool *));

memset(nulls, false, natts * sizeof(nulls));

values[0] = ...GetDatum(...my datum...);
...
values[natts-1] = ...GetDatum(...my datum...);
...
tuple = heap_form_tuple(tupDesc, values, nulls);

------------------

When I come to last line, I get these errors (I'm using valgrind):

==25850== Source and destination overlap in memcpy(0x8BCB070, 0x4CF2480, 496753820)
==25850==    at 0x4024586: memcpy (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==25850==    by 0x808C811: heap_fill_tuple (in /usr/local/pgsql/bin/postgres)
==25850==    by 0x808D72F: heap_form_tuple (in /usr/local/pgsql/bin/postgres)
...
==25850== Invalid read of size 1
==25850==    at 0x40245A1: memcpy (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==25850==    by 0x808C811: heap_fill_tuple (in /usr/local/pgsql/bin/postgres)
==25850==    by 0x808D72F: heap_form_tuple (in /usr/local/pgsql/bin/postgres)
...
==25850==  Address 0x8BCB027 is 1 bytes before a block of size 496,753,892 alloc'd
==25850==    at 0x4022825: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==25850==    by 0x82BE278: AllocSetAlloc (in /usr/local/pgsql/bin/postgres)
==25850==    by 0x82BEE91: MemoryContextAllocZero (in /usr/local/pgsql/bin/postgres)
==25850==    by 0x808D694: heap_form_tuple (in /usr/local/pgsql/bin/postgres)
...
==25850==  Address 0x8BCB027 is 2 bytes before a block of size 496,753,892 alloc'd
...
==25850==  Address 0x8BCB027 is 3 bytes before a block of size 496,753,892 alloc'd
...
==25850==  Address 0x8BCB027 is 4 bytes before a block of size 496,753,892 alloc'd
...
==25850== Process terminating with default action of signal 11 (SIGSEGV)
==25850==  Access not within mapped region at address 0x8BCAFFF
==25850==    at 0x40245A1: memcpy (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==25850==    by 0x808C811: heap_fill_tuple (in /usr/local/pgsql/bin/postgres)
==25850==    by 0x808D72F: heap_form_tuple (in /usr/local/pgsql/bin/postgres)

If I work on tables with only 1 attribute, I use normal variables (Datum values, bool nulls) instead of arrays, and
usingheap_form_tuple(TupleDesc td, &values, &nulls) works flawlessly. What am i missing? 

Thanks in advance for help,
Claudio Rossi




pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: [PATCHES] libpq type system 0.9a
Next
From: "Pavan Deolasee"
Date:
Subject: Re: Segfault using heap_form_tuple