Skip to content

Commit 31786ee

Browse files
[flang] Avoid undefined behaviour in Interval::Contains (#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
1 parent 9006bc8 commit 31786ee

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

flang/include/flang/Common/interval.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ template <typename A> class Interval {
6060
return start_ <= x && x < start_ + size_;
6161
}
6262
constexpr bool Contains(const Interval &that) const {
63-
return Contains(that.start_) && Contains(that.start_ + (that.size_ - 1));
63+
return Contains(that.start_) &&
64+
((that.size_ == 0) || Contains(that.start_ + (that.size_ - 1)));
6465
}
6566
constexpr bool IsDisjointWith(const Interval &that) const {
6667
return that.NextAfter() <= start_ || NextAfter() <= that.start_;

0 commit comments

Comments
 (0)