Copilot commented on code in PR #1620:
URL: https://github.com/apache/cloudberry/pull/1620#discussion_r2933751299


##########
src/backend/optimizer/util/clauses.c:
##########
@@ -5522,35 +5522,40 @@ flatten_join_alias_var_optimizer(Query *query, int 
queryLevel)
        if (NIL != targetList)
        {
                queryNew->targetList = (List *) 
flatten_join_alias_vars(queryNew, (Node *) targetList);
-               list_free(targetList);
+               if (targetList != queryNew->targetList)
+                       list_free(targetList);
        }
 
-       List * returningList = queryNew->returningList;
+       List *returningList = queryNew->returningList;
        if (NIL != returningList)
        {
                queryNew->returningList = (List *) 
flatten_join_alias_vars(queryNew, (Node *) returningList);
-               list_free(returningList);
+               if (returningList != queryNew->returningList)
+                       list_free(returningList);
        }
 
        Node *havingQual = queryNew->havingQual;
        if (NULL != havingQual)
        {
                queryNew->havingQual = flatten_join_alias_vars(queryNew, 
havingQual);
-               pfree(havingQual);
+               if (havingQual != queryNew->havingQual)
+                       pfree(havingQual);

Review Comment:
   `havingQual` is documented/used in other parts of the planner as an 
implicit-AND *list* of quals (often a `List *`). If `flatten_join_alias_vars()` 
returns a new pointer and the old `havingQual` was a `List`, calling 
`pfree(havingQual)` only frees the list header and leaks the list cells. 
Consider freeing based on the actual node type (e.g., `list_free()` when it’s a 
`List`, otherwise `pfree()`), while still keeping the pointer-equality guard to 
avoid the use-after-free.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to