From ff3913ad999a3a19ade60e2bd1b4c640993cf8f8 Mon Sep 17 00:00:00 2001 From: Onur Tirtir Date: Mon, 24 Jan 2022 17:27:41 +0300 Subject: [PATCH] Copy errmsg for distributed deadlock error into heap (#5641) multi_log_hook() hook is called by EmitErrorReport() when emitting the ereport either to frontend or to the server logs. And some callers of EmitErrorReport() (e.g.: errfinish()) seems to assume that string fields of given ErrorData object needs to be freed. For this reason, we copy the message into heap here. I don't think we have faced with such a problem before but it seems worth fixing as it is theoretically possible due to the reasoning above. --- src/backend/distributed/shared_library_init.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/backend/distributed/shared_library_init.c b/src/backend/distributed/shared_library_init.c index f74a3f43b..5e3ab85d2 100644 --- a/src/backend/distributed/shared_library_init.c +++ b/src/backend/distributed/shared_library_init.c @@ -446,8 +446,16 @@ multi_log_hook(ErrorData *edata) MyBackendGotCancelledDueToDeadlock(clearState)) { edata->sqlerrcode = ERRCODE_T_R_DEADLOCK_DETECTED; - edata->message = "canceling the transaction since it was " - "involved in a distributed deadlock"; + + /* + * This hook is called by EmitErrorReport() when emitting the ereport + * either to frontend or to the server logs. And some callers of + * EmitErrorReport() (e.g.: errfinish()) seems to assume that string + * fields of given ErrorData object needs to be freed. For this reason, + * we copy the message into heap here. + */ + edata->message = pstrdup("canceling the transaction since it was " + "involved in a distributed deadlock"); } }