Skip to content

Commit

Permalink
Add shmem check in SQL function to avoid segmentation fault
Browse files Browse the repository at this point in the history
  • Loading branch information
kovmir committed Mar 14, 2023
1 parent 7654169 commit 578995a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pg_show_plans.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ static void cleanup(int code, Datum arg);
static void set_state(const bool state);
/* Set query plan output format: text, json, ... */
static void set_format(const int format);
/* Check the extension has been properly loaded. */
static inline void shmem_safety_check(void);
/* Check whether the user has required privileges. */
static bool is_allowed_role(void);
/* Hook functions. */
Expand Down Expand Up @@ -324,17 +326,31 @@ void cleanup(int code, Datum arg)
void
set_state(const bool state)
{
shmem_safety_check();
if (is_allowed_role())
pgsp->is_enabled = state;
}

void
set_format(const int format)
{
shmem_safety_check();
if (is_allowed_role())
pgsp->plan_format = format;
}

inline void
shmem_safety_check(void)
{
if (pgsp && pgsp_hash)
return;

ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("pg_show_plans must be loaded"
"via shared_preload_libraries")));
}

bool
is_allowed_role(void)
{
Expand Down Expand Up @@ -520,6 +536,8 @@ pg_show_plans(PG_FUNCTION_ARGS)
int curr_nest;
bool is_done;

shmem_safety_check();

if (SRF_IS_FIRSTCALL())
{
MemoryContext oldcontext;
Expand Down

0 comments on commit 578995a

Please sign in to comment.