[flang-commits] [flang] [Flang][OpenMP] Fix crash on invalid atomic variable expressions (PR #173068)

via flang-commits flang-commits at lists.llvm.org
Fri Dec 19 10:10:01 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-openmp

Author: Krish Gupta (KrxGu)

<details>
<summary>Changes</summary>

Fixes #<!-- -->169484

The compiler crashed with an assertion failure when processing OpenMP ATOMIC constructs containing invalid variable expressions like function references or undeclared variables.

## Root Cause: 
`CheckAtomicVariable()` assumed `GetAllDesignators()` always returns exactly one designator, but it returns an empty vector for function references.

## Fix: 
Replaced the assertion with proper validation that emits diagnostic errors instead of crashing.

## Testing: 
Added regression test [atomic-invalid-variable.f90]

<img width="733" height="303" alt="image" src="https://github.com/user-attachments/assets/1dead98a-4012-45a6-8233-2ac0b0678906" />


---
Full diff: https://github.com/llvm/llvm-project/pull/173068.diff


2 Files Affected:

- (modified) flang/lib/Semantics/check-omp-atomic.cpp (+18-1) 
- (added) flang/test/Semantics/OpenMP/atomic-invalid-variable.f90 (+33) 


``````````diff
diff --git a/flang/lib/Semantics/check-omp-atomic.cpp b/flang/lib/Semantics/check-omp-atomic.cpp
index b9e34ca6e74df..01d3db3dafd0d 100644
--- a/flang/lib/Semantics/check-omp-atomic.cpp
+++ b/flang/lib/Semantics/check-omp-atomic.cpp
@@ -587,7 +587,24 @@ void OmpStructureChecker::CheckAtomicVariable(
   }
 
   std::vector<SomeExpr> dsgs{GetAllDesignators(atom)};
-  assert(dsgs.size() == 1 && "Should have a single top-level designator");
+
+  // Validate that we have exactly one designator
+  if (dsgs.empty()) {
+    // No designators found - likely a function reference or invalid expression
+    context_.Say(source,
+        "Invalid atomic variable '%s' - expected a variable designator"_err_en_US,
+        atom.AsFortran());
+    return;
+  }
+
+  if (dsgs.size() > 1) {
+    // Multiple designators - shouldn't happen for valid atomic variables
+    context_.Say(source,
+        "Atomic variable '%s' has multiple designators"_err_en_US,
+        atom.AsFortran());
+    return;
+  }
+
   evaluate::SymbolVector syms{evaluate::GetSymbolVector(dsgs.front())};
 
   CheckAtomicType(syms.back(), source, atom.AsFortran(), checkTypeOnPointer);
diff --git a/flang/test/Semantics/OpenMP/atomic-invalid-variable.f90 b/flang/test/Semantics/OpenMP/atomic-invalid-variable.f90
new file mode 100644
index 0000000000000..eac8077650a39
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/atomic-invalid-variable.f90
@@ -0,0 +1,33 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
+! Test for issue #169484: Flang should not crash on invalid atomic variables
+! This test verifies that proper diagnostics are emitted for invalid atomic
+! constructs instead of crashing the compiler.
+
+subroutine test_atomic_invalid(a, b, i)
+  integer :: i, a, b
+  interface
+    function baz(i) result(res)
+      integer :: i, res
+    end function
+  end interface
+  
+  ! Valid atomic update - should work fine
+  !$omp atomic
+    b = b + a
+  
+  ! Invalid: z is undeclared, so z(1) is treated as a function reference
+  ! This should emit an error, not crash
+  !$omp atomic
+    !ERROR: Left-hand side of assignment is not definable
+    !ERROR: 'z(1_4)' is not a variable or pointer
+    !ERROR: Invalid atomic variable 'z(1_4)' - expected a variable designator
+    z(1) = z(1) + 1
+  
+  ! Invalid: baz(i) is a function reference, not a variable
+  ! This should emit an error, not crash
+  !$omp atomic
+    !ERROR: Left-hand side of assignment is not definable
+    !ERROR: 'baz(i)' is not a variable or pointer
+    !ERROR: This is not a valid ATOMIC UPDATE operation
+    baz(i) = 1
+end subroutine

``````````

</details>


https://github.com/llvm/llvm-project/pull/173068


More information about the flang-commits mailing list