postgresql/src/backend
David Rowley f0efa5aec1 Introduce the concept of read-only StringInfos
There were various places in our codebase which conjured up a StringInfo
by manually assigning the StringInfo fields and setting the data field
to point to some existing buffer.  There wasn't much consistency here as
to what fields like maxlen got set to and in one location we didn't
correctly ensure that the buffer was correctly NUL terminated at len
bytes, as per what was documented as required in stringinfo.h

Here we introduce 2 new functions to initialize StringInfos.  One allows
callers to initialize a StringInfo passing along a buffer that is
already allocated by palloc.  Here the StringInfo code uses this buffer
directly rather than doing any memcpying into a new allocation.  Having
this as a function allows us to verify the buffer is correctly NUL
terminated.  StringInfos initialized this way can be appended to and
reset just like any other normal StringInfo.

The other new initialization function also accepts an existing buffer,
but the given buffer does not need to be a pointer to a palloc'd chunk.
This buffer could be a pointer pointing partway into some palloc'd chunk
or may not even be palloc'd at all.  StringInfos initialized this way
are deemed as "read-only".  This means that it's not possible to
append to them or reset them.

For the latter of the two new initialization functions mentioned above,
we relax the requirement that the data buffer must be NUL terminated.
Relaxing this requirement is convenient in a few places as it can save
us from having to allocate an entire new buffer just to add the NUL
terminator or save us from having to temporarily add a NUL only to have to
put the original char back again later.

Incompatibility note:

Here we also forego adding the NUL in a few places where it does not
seem to be required.  These locations are passing the given StringInfo
into a type's receive function.  It does not seem like any of our
built-in receive functions require this, but perhaps there's some UDT
out there in the wild which does require this.  It is likely worthy of
a mention in the release notes that a UDT's receive function mustn't rely
on the input StringInfo being NUL terminated.

Author: David Rowley
Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/CAApHDvorfO3iBZ%3DxpiZvp3uHtJVLyFaPBSvcAhAq2HPLnaNSwQ%40mail.gmail.com
2023-10-26 16:31:48 +13:00
..
access Assert that buffers are marked dirty before XLogRegisterBuffer(). 2023-10-23 17:17:46 -07:00
archive Redesign archive modules 2023-02-17 14:26:42 +09:00
backup Change struct tablespaceinfo's oid member from 'char *' to 'Oid' 2023-10-23 15:17:26 -04:00
bootstrap Refactor InitPostgres() to use bitwise option flags 2023-10-11 12:31:49 +09:00
catalog Allow ALTER SYSTEM to set unrecognized custom GUCs. 2023-10-21 13:35:19 -04:00
commands Make some error strings more generic 2023-10-20 22:52:15 +02:00
executor Fix problems when a plain-inheritance parent table is excluded. 2023-10-24 14:48:33 -04:00
foreign Expand some more uses of "deleg" to "delegation" or "delegated". 2023-05-21 10:55:18 -04:00
jit jit: Changes for LLVM 17. 2023-10-19 05:13:23 +13:00
lib Make binaryheap available to frontend code. 2023-09-18 12:18:33 -07:00
libpq Refactor ListenSocket array. 2023-10-05 15:05:25 +03:00
main Remove obsolete defense against strxfrm() bugs. 2023-04-20 13:20:14 +12:00
nodes Remove IndexInfo.ii_OpclassOptions field 2023-10-03 17:51:02 +02:00
optimizer Prevent duplicate RTEPermissionInfo for plain-inheritance parents 2023-10-26 11:53:56 +09:00
parser Extend ALTER OPERATOR to allow setting more optimization attributes. 2023-10-20 12:28:46 -04:00
partitioning Fix incorrect step generation in HASH partition pruning 2023-10-12 19:50:38 +13:00
po Translation updates 2023-08-07 12:39:30 +02:00
port Add GUC parameter "huge_pages_status" 2023-07-06 14:42:36 +09:00
postmaster Avoid calling proc_exit() in processes forked by system(). 2023-10-17 10:41:48 -05:00
regex All supported systems have locale_t. 2023-07-09 11:55:18 +12:00
replication Introduce the concept of read-only StringInfos 2023-10-26 16:31:48 +13:00
rewrite Add TupleDescGetDefault() 2023-09-27 18:52:40 +01:00
snowball Fix the install rule for snowball_create.sql. 2023-05-23 11:15:57 -04:00
statistics Take pg_attribute out of VacAttrStats 2023-07-03 07:18:57 +02:00
storage Assert that buffers are marked dirty before XLogRegisterBuffer(). 2023-10-23 17:17:46 -07:00
tcop Introduce the concept of read-only StringInfos 2023-10-26 16:31:48 +13:00
tsearch Limit to_tsvector_byid's initial array allocation to something sane. 2023-09-25 11:50:28 -04:00
utils Introduce the concept of read-only StringInfos 2023-10-26 16:31:48 +13:00
.gitignore
common.mk Blind attempt to fix LLVM dependency in the backend 2022-09-15 10:53:48 +07:00
Makefile Add system view pg_wait_events 2023-08-20 15:35:02 +09:00
meson.build Add win32ver data to meson-built postgres.exe. 2023-06-12 07:40:38 -07:00
nls.mk Add missing gettext triggers 2023-05-10 13:51:51 +02:00