Re: Can we modify 'text *' passed to a C function?

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Can we modify 'text *' passed to a C function?
Дата
Msg-id 18988.981421324@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Can we modify 'text *' passed to a C function?  (m w <mttf2000@yahoo.com>)
Ответы Re: Can we modify 'text *' passed to a C function?  (m w <mttf2000@yahoo.com>)
Список 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 по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Re: [BUGS] syslog logging setup broken?
Следующее
От: Tom Lane
Дата:
Сообщение: Re: pg_dump data integrity & java