Segfault using heap_form_tuple

From: "Claudio Rossi" <wind(dot)claudio(at)inwind(dot)it>
To: "pgsql-hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Segfault using heap_form_tuple
Date: 2008-04-09 17:18:14
Message-ID: JZ2IQE$322CDC8D53003B9CD19D97C281282182@libero.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 code looks 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 using heap_form_tuple(TupleDesc td, &values, &nulls) works flawlessly. What am i missing?

Thanks in advance for help,
Claudio Rossi

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavan Deolasee 2008-04-09 17:27:09 Re: Segfault using heap_form_tuple
Previous Message Tom Lane 2008-04-09 17:08:15 Re: [PATCHES] libpq type system 0.9a