Skip to content

Commit 4662cc6

Browse files
committed
fix possible plpgsql_check crash, disallow late pldbapi2 initialization
1 parent a3681e5 commit 4662cc6

5 files changed

+10
-55
lines changed

expected/plpgsql_check_active.out

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9421,9 +9421,4 @@ $$ language plpgsql;
94219421
\c
94229422
-- should not to crash
94239423
select trace_test(3);
9424-
WARNING: late initialization of fmgr_plpgsql_cache
9425-
trace_test
9426-
------------
9427-
3
9428-
(1 row)
9429-
9424+
ERROR: too late initialization of fmgr_plpgsql_cache

expected/plpgsql_check_active_1.out

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9423,9 +9423,4 @@ $$ language plpgsql;
94239423
\c
94249424
-- should not to crash
94259425
select trace_test(3);
9426-
WARNING: late initialization of fmgr_plpgsql_cache
9427-
trace_test
9428-
------------
9429-
3
9430-
(1 row)
9431-
9426+
ERROR: too late initialization of fmgr_plpgsql_cache

expected/plpgsql_check_active_2.out

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9420,9 +9420,4 @@ $$ language plpgsql;
94209420
\c
94219421
-- should not to crash
94229422
select trace_test(3);
9423-
WARNING: late initialization of fmgr_plpgsql_cache
9424-
trace_test
9425-
------------
9426-
3
9427-
(1 row)
9428-
9423+
ERROR: too late initialization of fmgr_plpgsql_cache

expected/plpgsql_check_active_3.out

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9421,9 +9421,4 @@ $$ language plpgsql;
94219421
\c
94229422
-- should not to crash
94239423
select trace_test(3);
9424-
WARNING: late initialization of fmgr_plpgsql_cache
9425-
trace_test
9426-
------------
9427-
3
9428-
(1 row)
9429-
9424+
ERROR: too late initialization of fmgr_plpgsql_cache

src/pldbgapi2.c

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -834,40 +834,15 @@ pldbgapi2_func_setup(PLpgSQL_execstate *estate, PLpgSQL_function *func)
834834
* in expected order (usually when plpgsql_check is initialized
835835
* inside function.
836836
*/
837-
if (!fcache_plpgsql)
837+
if (!fcache_plpgsql ||
838+
fcache_plpgsql->magic != FMGR_CACHE_MAGIC ||
839+
!fcache_plpgsql->is_plpgsql)
838840
{
839-
ereport(WARNING,
840-
(errmsg("late initialization of fmgr_plpgsql_cache"),
841-
errhint("use \"load 'plpgsql_check'\" before you try to use pldbgapi2")));
842-
843-
/*
844-
* Unfortunately, we have not access to fmgr context, so we should
845-
* to use top memory context. This is permament leak, but only for
846-
* few calls until fmgr hook will be correctly used.
847-
*/
848-
oldcxt = MemoryContextSwitchTo(TopMemoryContext);
849-
850-
fcache_plpgsql = palloc0(sizeof(fmgr_plpgsql_cache));
851-
852-
fcache_plpgsql->magic = FMGR_CACHE_MAGIC;
853-
854-
fcache_plpgsql->funcid = func->fn_oid;
855-
856-
fcache_plpgsql->is_plpgsql = true;
857-
fcache_plpgsql->fn_mcxt = CurrentMemoryContext;
858-
fcache_plpgsql->stmtid_stack = palloc_array(int, INITIAL_PLDBGAPI2_STMT_STACK_SIZE);
859-
fcache_plpgsql->stmtid_stack_size = INITIAL_PLDBGAPI2_STMT_STACK_SIZE;
860-
fcache_plpgsql->current_stmtid_stack_size = 0;
861-
862-
last_fmgr_plpgsql_cache = fcache_plpgsql;
863-
864-
MemoryContextSwitchTo(oldcxt);
841+
ereport(ERROR,
842+
(errmsg("too late initialization of fmgr_plpgsql_cache"),
843+
errhint("Use \"load 'plpgsql_check'\" before you use plpgsql_check functionality.")));
865844
}
866845

867-
Assert(fcache_plpgsql->magic == FMGR_CACHE_MAGIC);
868-
Assert(fcache_plpgsql);
869-
Assert(fcache_plpgsql->is_plpgsql);
870-
871846
#ifdef USE_ASSERT_CHECKING
872847

873848
if (fcache_plpgsql->funcid != PLpgSQLinlineFunc)

0 commit comments

Comments
 (0)