pgsql: Clean up memory leakage that occurs in context callback function - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Clean up memory leakage that occurs in context callback function
Date
Msg-id E1v6wST-000svm-0Z@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Clean up memory leakage that occurs in context callback functions.

An error context callback function might leak some memory into
ErrorContext, since those functions are run with ErrorContext as
current context.  In the case where the elevel is ERROR, this is
no problem since the code level that catches the error should do
FlushErrorState to clean up, and that will reset ErrorContext.
However, if the elevel is less than ERROR then no such cleanup occurs.
In principle, repeated leaks while emitting log messages or client
notices could accumulate arbitrarily much leaked data, if no ERROR
occurs in the session.

To fix, let errfinish() perform an ErrorContext reset if it is
at the outermost error nesting level.  (If it isn't, we'll delay
cleanup until the outermost nesting level is exited.)

The only actual leakage of this sort that I've been able to observe
within our regression tests was recently introduced by commit
f727b63e8.  While it seems plausible that there are other such
leaks not reached in the regression tests, the lack of field
reports suggests that they're not a big problem.  Accordingly,
I won't take the risk of back-patching this now.  We can always
back-patch later if we get field reports of leaks.

Reported-by: Andres Freund <andres@anarazel.de>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/jngsjonyfscoont4tnwi2qoikatpd5hifsg373vmmjvugwiu6g@m6opxh7uisgd

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/89d57c1fb35522590ec1f70b123c853cf5a9acb2

Modified Files
--------------
src/backend/utils/error/elog.c | 17 +++++++++++++----
src/backend/utils/misc/guc.c   | 10 +++++-----
2 files changed, 18 insertions(+), 9 deletions(-)


pgsql-committers by date:

Previous
From: Masahiko Sawada
Date:
Subject: pgsql: Fix access-to-already-freed-memory issue in pgoutput.
Next
From: David Rowley
Date:
Subject: pgsql: Cleanup VACUUM option processing error messages