[flang-commits] [flang] [flang] Avoid undefined behaviour in Interval::Contains (PR #147505)
David Spickett via flang-commits
flang-commits at lists.llvm.org
Tue Jul 8 04:01:58 PDT 2025
https://github.com/DavidSpickett created https://github.com/llvm/llvm-project/pull/147505
If the size of the other Interval was 0, (that.size_ - 1) would wrap below zero.
I've fixed this so that a zero size interval A is within interval B if the start of A is within B. There's a few ways you could handle zero sized intervals in theory but this one passes all tests so I assume it's the intention.
This fixes the following tests when ubsan is enabled:
Flang :: Lower/OpenMP/PFT/sections-pft.f90
Flang :: Lower/OpenMP/derived-type-allocatable.f90
Flang :: Lower/OpenMP/privatization-proc-ptr.f90
Flang :: Lower/OpenMP/sections.f90
Flang :: Parser/OpenMP/sections.f90
Flang :: Semantics/OpenMP/clause-validity01.f90
Flang :: Semantics/OpenMP/if-clause.f90
Flang :: Semantics/OpenMP/parallel-sections01.f90
Flang :: Semantics/OpenMP/private-assoc.f90
>From 128db9d580e102282cf820c3e21ffb62af8e217c Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Tue, 8 Jul 2025 10:57:14 +0000
Subject: [PATCH] [flang] Avoid undefined behaviour in Interval::Contains
If the size of the other Interval was 0, (that.size_ - 1)
would wrap below zero.
I've fixed this so that a zero size interval A is within
interval B if the start of A is within B.
There's a few ways you could handle zero sized intervals
in theory but this one passes all tests so I assume it's
the intention.
This fixes the following tests when ubsan is enabled:
Flang :: Lower/OpenMP/PFT/sections-pft.f90
Flang :: Lower/OpenMP/derived-type-allocatable.f90
Flang :: Lower/OpenMP/privatization-proc-ptr.f90
Flang :: Lower/OpenMP/sections.f90
Flang :: Parser/OpenMP/sections.f90
Flang :: Semantics/OpenMP/clause-validity01.f90
Flang :: Semantics/OpenMP/if-clause.f90
Flang :: Semantics/OpenMP/parallel-sections01.f90
Flang :: Semantics/OpenMP/private-assoc.f90
---
flang/include/flang/Common/interval.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/flang/include/flang/Common/interval.h b/flang/include/flang/Common/interval.h
index c4cab0ccf1130..2f0b6d9b55fe5 100644
--- a/flang/include/flang/Common/interval.h
+++ b/flang/include/flang/Common/interval.h
@@ -60,7 +60,8 @@ template <typename A> class Interval {
return start_ <= x && x < start_ + size_;
}
constexpr bool Contains(const Interval &that) const {
- return Contains(that.start_) && Contains(that.start_ + (that.size_ - 1));
+ return Contains(that.start_) &&
+ ((that.size_ == 0) || Contains(that.start_ + (that.size_ - 1)));
}
constexpr bool IsDisjointWith(const Interval &that) const {
return that.NextAfter() <= start_ || NextAfter() <= that.start_;
More information about the flang-commits
mailing list