Skip to content

Conversation

@mohsinm-dev
Copy link

Summary

Fixes the segmentation fault that occurs when Clad attempts to differentiate for loops with empty increment expressions.

Problem

Issue #1436 reported that Clad would crash with a segfault when processing code like:

for (int i = 0; i != 1 ;) {  // Empty increment
     sum += u + v;
     break;
 }

The crash occurred in ReverseModeVisitor::VisitForStmt() when:

  1. dyn_cast(FS->getInc()) was called on nullptr
  2. cast(incDiff.getStmt()) was called on uninitialized StmtDiff

Solution

Added defensive null checks before processing increment expressions:

  • Check FS->getInc() before calling dyn_cast
  • Guard increment processing with proper null checks
  • Ensure compound statement casting is safe

Testing

  • ✅ Original failing case now compiles without segfault
  • ✅ Empty increment loops produce correct derivatives
  • ✅ Normal increment loops work as before (regression test)
  • ✅ Added comprehensive test in test/Misc/LoopIncrementFix.C

Changes

  • lib/Differentiator/ReverseModeVisitor.cpp: Added null checks for increment handling
  • test/Misc/LoopIncrementFix.C: Added test case to prevent regression

Fixes #1436

@github-actions
Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

Resolves issue vgvassilev#1442 where Clad would crash with a segfault when
differentiating functions containing assert statements. The crash
occurred in ReferencesUpdater::updateType() when processing
SourceLocExpr nodes that return null QualType values.

Changes:
- Add null checks in ReferencesUpdater::VisitStmt() before calling updateType()
- Add defensive null check in ReferencesUpdater::VisitDeclRefExpr() for consistency
- Add test case to prevent regression

Fixes vgvassilev#1442
Resolves issue vgvassilev#1436 where Clad would crash when differentiating
for loops with empty increment expressions (e.g., for(int i=0; i!=1;)).

The crash occurred in ReverseModeVisitor::VisitForStmt() when:
1. Line 1029: dyn_cast<DeclRefExpr>(FS->getInc()) on nullptr
2. Line 1042: cast<CompoundStmt>(incDiff.getStmt()) on uninitialized StmtDiff

Changes:
- Add null check before dyn_cast in increment expression handling
- Add null checks before processing increment statement diffs
- Protect compound statement casting with proper conditions
- Add test case for empty increment loops

Fixes vgvassilev#1436
The activity.md file will be used to track personal development
progress, issue analysis, and fixes but should not be part of
the main repository.
The CI was failing because our tests generated expected warnings
but used the wrong FileCheck pattern. Fixed to use:
- 'expected-warning' comments for anticipated warnings
- 'expected-no-diagnostics' for tests with no warnings
- '-Xclang -verify' and '-fsyntax-only' for proper testing

This follows the established Clad testing patterns.
@vgvassilev vgvassilev force-pushed the fix-loop-increment-crash-1436 branch 2 times, most recently from 2849bcd to d60f23b Compare July 31, 2025 18:47
@github-actions
Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clad crashes if the loop increment expression is empty

1 participant