https://github.com/NagyDonat created 
https://github.com/llvm/llvm-project/pull/241415

The method `CheckerContext::addTransition` should create a new exploded node 
when (1) there was a state change or (2) an explicit non-null `Tag` is 
specified.

However, in the 3-parameter overload (which accepts an exploded node `Pred` to 
allow chaining transitions) this logic compared the `State` to `getState()` 
(the state at the construction of `CheckerContext`) and returned early with 
`getPredecessor()` (the node that was passed to the constructor of 
`CheckerContext`) instead of `Pred`.

This commit corrects this oversight by using `Pred` and its state -- but 
preserves the corner case that a null `Pred` is accepted and handled as if it 
was `getPredecessor()`. (Note that passing a null node to `addTransitionImpl` 
is equivalent to passing `getPredecessor()`, some checkers -- at least 
`FuchsiaHandleChecker` leak reporting -- rely on that `addTransition` can 
gracefully handle a null node argument.)

This may perturb node creation in rare cases (within the LIT tests there were 
two occurrences of the same difference: the new early return activated in 
`MPIChecker` in cases where the old one didn't fire) but I don't expect a 
significant impact. I verified that `MPIChecker` will function correctly with 
the new behavior of`addTransition`.

From 226c1d82f1a16b5b0bf022a6684e951a99f2d1ca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <[email protected]>
Date: Wed, 11 Nov 2026 16:07:38 +0100
Subject: [PATCH] [analyzer] Correct logic in 3-param addTransition

The method `CheckerContext::addTransition` should create a new exploded
node when (1) there was a state change or (2) an explicit non-null `Tag`
is specified.

However, in the 3-parameter overload (which accepts an exploded node
`Pred` to allow chaining transitions) this logic compared the `State` to
`getState()` (the state at the construction of `CheckerContext`) and
returned early with `getPredecessor()` (the node that was passed to the
constructor of `CheckerContext`) instead of `Pred`.

This commit corrects this oversight by using `Pred` and its state -- but
preserves the corner case that a null `Pred` is accepted and handled as
if it was `getPredecessor()`. (Note that passing a null node to
`addTransitionImpl` is equivalent to passing `getPredecessor()`, some
checkers -- at least `FuchsiaHandleChecker` leak reporting -- rely on
that `addTransition` can gracefully handle a null node argument.)

This may perturb node creation in rare cases (within the LIT tests there
were two ocurrences of the same difference: the new early return
activated in `MPIChecker` in cases where the old one didn't fire) but I
don't expect a significant impact. I verified that `MPIChecker` will
function correctly with the new behavior of`addTransition`.
---
 .../Core/PathSensitive/CheckerContext.h       | 21 ++++++++-----------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git 
a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h 
b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
index eaf62e8c8fbb3b..237d733e9ea163 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
@@ -202,13 +202,13 @@ class CheckerContext {
   /// the parameter \p Pred. Allows checkers to make a chain of nodes.
   /// Can return nullptr if the node creation fails (when it would recreate
   /// an existing node or when the state is posteriorly overconstrained).
-  ///
-  /// Can return getPredecessor(), the node that was passed to the constructor
-  /// of CheckerContext (often the ancestor of \p Pred) in some corner cases.
-  /// FIXME: This is bad logic and should be fixed in the next non-NFC change.
+  /// Can return \p Pred instead of creating a new node when the state is null
+  /// or when the state would remain unchanged and there is no explicitly
+  /// specified tag.
   ///
   /// @param State The state of the generated node.
   /// @param Pred The node that will become the predecessor of the new node.
+  ///        When null, the method falls back to using 'getPredecessor()'.
   /// @param Tag A ProgramPointTag that will be attached to the node. This is
   ///        primarily useful for attaching a NoteTag; but another tag could be
   ///        used as a workaround when the node wouldn't be unique otherwise.
@@ -216,15 +216,12 @@ class CheckerContext {
   ///        active checker as the tag.
   ExplodedNode *addTransition(ProgramStateRef State, ExplodedNode *Pred,
                               const ProgramPointTag *Tag = nullptr) {
-    // FIXME: In these two early return statements we should use 'Pred' and
-    // 'Pred->getState()' (the node received as an argument and its state)
-    // instead of 'getPredecessor()' and 'getState()' (the older node received
-    // by the constructor of CheckerContext, and its state).
-    // Alternatively, we should perhaps return null if the state is null.
+    if (!Pred)
+      Pred = getPredecessor();
     if (!State)
-      return getPredecessor();
-    if (State == getState() && !Tag)
-      return getPredecessor();
+      return Pred;
+    if (State == Pred->getState() && !Tag)
+      return Pred;
     return addTransitionImpl(State, false, Pred, Tag);
   }
 

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to