Skip to content

Commit 578995a

Browse files
committed
Add shmem check in SQL function to avoid segmentation fault
1 parent 7654169 commit 578995a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pg_show_plans.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ static void cleanup(int code, Datum arg);
8888
static void set_state(const bool state);
8989
/* Set query plan output format: text, json, ... */
9090
static void set_format(const int format);
91+
/* Check the extension has been properly loaded. */
92+
static inline void shmem_safety_check(void);
9193
/* Check whether the user has required privileges. */
9294
static bool is_allowed_role(void);
9395
/* Hook functions. */
@@ -324,17 +326,31 @@ void cleanup(int code, Datum arg)
324326
void
325327
set_state(const bool state)
326328
{
329+
shmem_safety_check();
327330
if (is_allowed_role())
328331
pgsp->is_enabled = state;
329332
}
330333

331334
void
332335
set_format(const int format)
333336
{
337+
shmem_safety_check();
334338
if (is_allowed_role())
335339
pgsp->plan_format = format;
336340
}
337341

342+
inline void
343+
shmem_safety_check(void)
344+
{
345+
if (pgsp && pgsp_hash)
346+
return;
347+
348+
ereport(ERROR,
349+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
350+
errmsg("pg_show_plans must be loaded"
351+
"via shared_preload_libraries")));
352+
}
353+
338354
bool
339355
is_allowed_role(void)
340356
{
@@ -520,6 +536,8 @@ pg_show_plans(PG_FUNCTION_ARGS)
520536
int curr_nest;
521537
bool is_done;
522538

539+
shmem_safety_check();
540+
523541
if (SRF_IS_FIRSTCALL())
524542
{
525543
MemoryContext oldcontext;

0 commit comments

Comments
 (0)