Skip to content

Commit 87ca51a

Browse files
authored
[Flang][OpenMP] Fix crash on invalid atomic variable expressions (#173068)
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]
1 parent d22d2e3 commit 87ca51a

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

flang/lib/Semantics/check-omp-atomic.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,18 @@ void OmpStructureChecker::CheckAtomicVariable(
587587
}
588588

589589
std::vector<SomeExpr> dsgs{GetAllDesignators(atom)};
590-
assert(dsgs.size() == 1 && "Should have a single top-level designator");
590+
591+
// Procedure references are valid if they return a pointer to a scalar.
592+
// Just return if we don't have exactly one designator - other checks will
593+
// diagnose any actual errors.
594+
if (dsgs.size() != 1) {
595+
return;
596+
}
597+
591598
evaluate::SymbolVector syms{evaluate::GetSymbolVector(dsgs.front())};
599+
if (syms.empty()) {
600+
return;
601+
}
592602

593603
CheckAtomicType(syms.back(), source, atom.AsFortran(), checkTypeOnPointer);
594604

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
2+
! Test for issue #169484: Flang should not crash on invalid atomic variables
3+
! This test verifies that proper diagnostics are emitted for invalid atomic
4+
! constructs instead of crashing the compiler.
5+
6+
subroutine test_atomic_invalid(a, b, i)
7+
integer :: i, a, b
8+
interface
9+
function baz(i) result(res)
10+
integer :: i, res
11+
end function
12+
end interface
13+
14+
! Valid atomic update - should work fine
15+
!$omp atomic
16+
b = b + a
17+
18+
! Invalid: z is undeclared, so z(1) is treated as a function reference
19+
! This should emit an error, not crash
20+
!$omp atomic
21+
!ERROR: Left-hand side of assignment is not definable
22+
!ERROR: 'z(1_4)' is not a variable or pointer
23+
z(1) = z(1) + 1
24+
25+
! Invalid: baz(i) is a function reference, not a variable
26+
! This should emit an error, not crash
27+
!$omp atomic
28+
!ERROR: Left-hand side of assignment is not definable
29+
!ERROR: 'baz(i)' is not a variable or pointer
30+
!ERROR: This is not a valid ATOMIC UPDATE operation
31+
baz(i) = 1
32+
end subroutine

0 commit comments

Comments
 (0)