postgresql/src
David Rowley e629846472 Fix incorrect accessing of pfree'd memory in Memoize
For pass-by-reference types, the code added in 0b053e78b, which aimed to
resolve a memory leak, was overly aggressive in resetting the per-tuple
memory context which could result in pfree'd memory being accessed
resulting in failing to find previously cached results in the hash
table.

What was happening was prepare_probe_slot() was switching to the
per-tuple memory context and calling ExecEvalExpr().  ExecEvalExpr() may
have required a memory allocation.  Both MemoizeHash_hash() and
MemoizeHash_equal() were aggressively resetting the per-tuple context
and after determining the hash value, the context would have gotten reset
before MemoizeHash_equal() was called.  This could have resulted in
MemoizeHash_equal() looking at pfree'd memory.

This is less likely to have caused issues on a production build as some
other allocation would have had to have reused the pfree'd memory to
overwrite it.  Otherwise, the original contents would have been intact.
However, this clearly caused issues on MEMORY_CONTEXT_CHECKING builds.

Author: Tender Wang, Andrei Lepikhov
Reported-by: Tender Wang (using SQLancer)
Reviewed-by: Andrei Lepikhov, Richard Guo, David Rowley
Discussion: https://postgr.es/m/CAHewXNnT6N6UJkya0z-jLFzVxcwGfeRQSfhiwA+NyLg-x8iGew@mail.gmail.com
Backpatch-through: 14, where Memoize was added
2024-03-11 18:19:56 +13:00
..
backend Fix incorrect accessing of pfree'd memory in Memoize 2024-03-11 18:19:56 +13:00
bin Catalog changes preparing for builtin collation provider. 2024-03-09 14:48:18 -08:00
common Fix type signedness error in commit 5c40364dd6. 2024-03-08 16:00:46 -08:00
fe_utils Centralize logic for restoring errno in signal handlers. 2024-02-14 16:34:18 -06:00
include Improve support for ExplainOneQuery() hook 2024-03-11 08:40:40 +09:00
interfaces Cope with a deficiency in OpenSSL 3.x's error reporting. 2024-03-07 19:38:17 -05:00
makefiles Remove AIX support 2024-02-28 15:17:23 +04:00
pl Redefine backend ID to be an index into the proc array 2024-03-03 19:37:28 +02:00
port Fix overflow in Windows replacement pg_pread/pg_pwrite. 2024-03-03 08:40:41 +13:00
template Remove AIX support 2024-02-28 15:17:23 +04:00
test Fix incorrect accessing of pfree'd memory in Memoize 2024-03-11 18:19:56 +13:00
timezone Update time zone data files to tzdata release 2024a. 2024-02-01 15:57:53 -05:00
tools Combine headerscheck and cpluspluscheck scripts 2024-03-10 07:56:17 +01:00
tutorial Update copyright for 2024 2024-01-03 20:49:05 -05:00
.gitignore
DEVELOPERS
Makefile
Makefile.global.in Remove make function vpathsearch 2024-01-29 07:24:59 +01:00
Makefile.shlib Remove AIX support 2024-02-28 15:17:23 +04:00
meson.build Update copyright for 2024 2024-01-03 20:49:05 -05:00
nls-global.mk