Skip to content

Commit 65cd966

Browse files
zhangyue-hashdatamy-ship-it
authored andcommitted
Fix double free issue in alterResgroupCallback during io_limit cleanup
We need to handle two scenarios: 1. When caps.io_limit differs from oldCaps.io_limit — this corresponds to the RESGROUP_LIMIT_TYPE_IO_LIMIT case. 2. When caps.io_limit is equal to oldCaps.io_limit — this applies to all other cases. The original code causes a double free issue in the second scenario ("other cases").
1 parent 2663671 commit 65cd966

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/backend/commands/resgroupcmds.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,10 +1093,23 @@ alterResgroupCallback(XactEvent event, void *arg)
10931093
if (event == XACT_EVENT_COMMIT)
10941094
ResGroupAlterOnCommit(callbackCtx);
10951095

1096+
/*
1097+
* Free io_limit resources allocated in AlterResourceGroup().
1098+
*
1099+
* We need to handle two cases:
1100+
* 1. caps.io_limit != oldCaps.io_limit: case RESGROUP_LIMIT_TYPE_IO_LIMIT
1101+
* 2. caps.io_limit == oldCaps.io_limit: other cases
1102+
*
1103+
* The pointer comparison (oldCaps.io_limit != caps.io_limit) is crucial to
1104+
* avoid double free errors. When "other cases", both pointers might
1105+
* reference the same memory location, so we only free oldCaps.io_limit if
1106+
* it's different from caps.io_limit.
1107+
*/
10961108
if (callbackCtx->caps.io_limit != NIL)
10971109
cgroupOpsRoutine->freeio(callbackCtx->caps.io_limit);
10981110

1099-
if (callbackCtx->caps.io_limit != NIL)
1111+
if (callbackCtx->oldCaps.io_limit != NIL &&
1112+
callbackCtx->oldCaps.io_limit != callbackCtx->caps.io_limit)
11001113
cgroupOpsRoutine->freeio(callbackCtx->oldCaps.io_limit);
11011114

11021115
pfree(callbackCtx);

0 commit comments

Comments
 (0)