Re: Can we modify 'text *' passed to a C function? - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Can we modify 'text *' passed to a C function?
Date
Msg-id 18988.981421324@sss.pgh.pa.us
Whole thread Raw
In response to Can we modify 'text *' passed to a C function?  (m w <mttf2000@yahoo.com>)
Responses Re: Can we modify 'text *' passed to a C function?  (m w <mttf2000@yahoo.com>)
List pgsql-hackers
m w <mttf2000@yahoo.com> writes:
> If I write a function that does a 'tolower()' on each
> character of a parameter passed into my function, do I
> have to allocate a new text object and initialize it
> and copy the data, or can I modify that object in
> place and return it?

You must NEVER alter a pass-by-reference parameter, of text or
any other data type.  If you do, you are probably altering data
in a disk buffer, thus changing the content of a tuple.

> Secondly, if I modify the data length of an object,
> as: VARDATA(field) = nn, making sure that 'nn' is
> smaller than the origial length, with this affect the
> memory management?

In a palloc'd temp object it's OK if the allocated size is more than
what you end up using, so yes you can set VARSIZE() to less than what
you allocated.  Some of the built-in functions do things this way to
simplify calculation of the palloc request size, IIRC.  Again, you
cannot scribble on a source object.

In 7.1, for toastable data types there are GETARG macros that guarantee
to return a writable copy, see PG_GETARG_TEXT_P_COPY etc.  I believe
tolower() and similar functions use this already.
        regards, tom lane


pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: Re: [BUGS] syslog logging setup broken?
Next
From: Tom Lane
Date:
Subject: Re: pg_dump data integrity & java