GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gimple-predicate-analysis

Fix shallow copy of pred_chain in predicate analysis

Corrects predicate analysis to avoid dangling pointers and double frees from shallow copies.

This commit fixes a bug in GCC’s gimple-predicate-analysis.cc where a shallow copy of a pred_chain was causing issues. The copy constructor for pred_chain shares the pointer to its elements, leading to a dangling pointer when the copy is released. This could result in a double free or reading from freed memory. The fix modifies simplify_4 to refer to the chain in place and adjusts simplify to correctly set the m_cval when a predicate folds to a constant value, resolving latent bugs exposed by recent changes.

In Details

In predicate::simplify_4, a shallow copy of m_preds[i] (a pred_chain, which is a vec) was created. The default copy constructor for vec performs a shallow copy, sharing the pointer to the underlying elements. Releasing this copy frees the buffer, leaving m_preds[i] dangling. The subsequent cleanup loop then accesses this dangling pointer. The fix ensures simplify_4 uses the original chain and simplify correctly handles constant folding of predicates by setting m_cval to avoid issues when disjunctions become TRUE or contradictions.

For Context
GIMPLE
GCC's internal three-address intermediate representation used for optimization.
predicate analysis
An analysis pass in GCC that determines properties or conditions related to program execution, often used for optimization.
pred_chain
A data structure within GCC's predicate analysis that likely represents a chain or sequence of predicates.
NULL_TREE
A special value in GCC's internal representation indicating an empty or absent tree node.
DCE
Dead Code Elimination, an optimization pass that removes code that does not affect the program's output.
Filed Under: optimizationbugfixgimple