Commit 7aa066f1 authored by Tom Lane's avatar Tom Lane

record_in and record_recv must be careful to return a separately

pfree'able result, since some callers expect to be able to pfree
the result of a pass-by-reference function.  Per report from Chris Trawick.
parent d3040676
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/rowtypes.c,v 1.8 2004/12/31 22:01:22 pgsql Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/rowtypes.c,v 1.9 2005/04/18 17:11:05 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -54,6 +54,7 @@ record_in(PG_FUNCTION_ARGS) ...@@ -54,6 +54,7 @@ record_in(PG_FUNCTION_ARGS)
{ {
char *string = PG_GETARG_CSTRING(0); char *string = PG_GETARG_CSTRING(0);
Oid tupType = PG_GETARG_OID(1); Oid tupType = PG_GETARG_OID(1);
HeapTupleHeader result;
int32 tupTypmod; int32 tupTypmod;
TupleDesc tupdesc; TupleDesc tupdesc;
HeapTuple tuple; HeapTuple tuple;
...@@ -244,11 +245,20 @@ record_in(PG_FUNCTION_ARGS) ...@@ -244,11 +245,20 @@ record_in(PG_FUNCTION_ARGS)
tuple = heap_formtuple(tupdesc, values, nulls); tuple = heap_formtuple(tupdesc, values, nulls);
/*
* We cannot return tuple->t_data because heap_formtuple allocates it
* as part of a larger chunk, and our caller may expect to be able to
* pfree our result. So must copy the info into a new palloc chunk.
*/
result = (HeapTupleHeader) palloc(tuple->t_len);
memcpy(result, tuple->t_data, tuple->t_len);
heap_freetuple(tuple);
pfree(buf.data); pfree(buf.data);
pfree(values); pfree(values);
pfree(nulls); pfree(nulls);
PG_RETURN_HEAPTUPLEHEADER(tuple->t_data); PG_RETURN_HEAPTUPLEHEADER(result);
} }
/* /*
...@@ -419,6 +429,7 @@ record_recv(PG_FUNCTION_ARGS) ...@@ -419,6 +429,7 @@ record_recv(PG_FUNCTION_ARGS)
{ {
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
Oid tupType = PG_GETARG_OID(1); Oid tupType = PG_GETARG_OID(1);
HeapTupleHeader result;
int32 tupTypmod; int32 tupTypmod;
TupleDesc tupdesc; TupleDesc tupdesc;
HeapTuple tuple; HeapTuple tuple;
...@@ -580,10 +591,19 @@ record_recv(PG_FUNCTION_ARGS) ...@@ -580,10 +591,19 @@ record_recv(PG_FUNCTION_ARGS)
tuple = heap_formtuple(tupdesc, values, nulls); tuple = heap_formtuple(tupdesc, values, nulls);
/*
* We cannot return tuple->t_data because heap_formtuple allocates it
* as part of a larger chunk, and our caller may expect to be able to
* pfree our result. So must copy the info into a new palloc chunk.
*/
result = (HeapTupleHeader) palloc(tuple->t_len);
memcpy(result, tuple->t_data, tuple->t_len);
heap_freetuple(tuple);
pfree(values); pfree(values);
pfree(nulls); pfree(nulls);
PG_RETURN_HEAPTUPLEHEADER(tuple->t_data); PG_RETURN_HEAPTUPLEHEADER(result);
} }
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment