Skip to content

Add checks that the endpoints of partial ranges are not-NaN. #33378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion stdlib/public/core/ClosedRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,12 @@ extension Comparable {
/// - Parameters:
/// - minimum: The lower bound for the range.
/// - maximum: The upper bound for the range.
///
/// - Precondition: `minimum <= maximum`.
@_transparent
public static func ... (minimum: Self, maximum: Self) -> ClosedRange<Self> {
_precondition(
minimum <= maximum, "Can't form Range with upperBound < lowerBound")
minimum <= maximum, "Range requires lowerBound <= upperBound")
return ClosedRange(uncheckedBounds: (lower: minimum, upper: maximum))
}
}
Expand Down
16 changes: 15 additions & 1 deletion stdlib/public/core/Range.swift
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,12 @@ extension Comparable {
/// - Parameters:
/// - minimum: The lower bound for the range.
/// - maximum: The upper bound for the range.
///
/// - Precondition: `minimum <= maximum`.
@_transparent
public static func ..< (minimum: Self, maximum: Self) -> Range<Self> {
_precondition(minimum <= maximum,
"Can't form Range with upperBound < lowerBound")
"Range requires lowerBound <= upperBound")
return Range(uncheckedBounds: (lower: minimum, upper: maximum))
}

Expand All @@ -752,8 +754,12 @@ extension Comparable {
/// // Prints "[10, 20, 30]"
///
/// - Parameter maximum: The upper bound for the range.
///
/// - Precondition: `maximum` must compare equal to itself (i.e. cannot be NaN).
@_transparent
public static prefix func ..< (maximum: Self) -> PartialRangeUpTo<Self> {
_precondition(maximum == maximum,
"Range cannot have an unordered upper bound.")
return PartialRangeUpTo(maximum)
}

Expand All @@ -779,8 +785,12 @@ extension Comparable {
/// // Prints "[10, 20, 30, 40]"
///
/// - Parameter maximum: The upper bound for the range.
///
/// - Precondition: `maximum` must compare equal to itself (i.e. cannot be NaN).
@_transparent
public static prefix func ... (maximum: Self) -> PartialRangeThrough<Self> {
_precondition(maximum == maximum,
"Range cannot have an unordered upper bound.")
return PartialRangeThrough(maximum)
}

Expand All @@ -806,8 +816,12 @@ extension Comparable {
/// // Prints "[40, 50, 60, 70]"
///
/// - Parameter minimum: The lower bound for the range.
///
/// - Precondition: `minimum` must compare equal to itself (i.e. cannot be NaN).
@_transparent
public static postfix func ... (minimum: Self) -> PartialRangeFrom<Self> {
_precondition(minimum == minimum,
"Range cannot have an unordered lower bound.")
return PartialRangeFrom(minimum)
}
}
Expand Down
40 changes: 40 additions & 0 deletions test/stdlib/RangeTraps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,47 @@ RangeTraps.test("CountablePartialRangeFrom")
_ = it.next()
}

RangeTraps.test("nanLowerBound")
.code {
expectCrashLater()
_ = Double.nan ... 0
}

RangeTraps.test("nanUpperBound")
.code {
expectCrashLater()
_ = 0 ... Double.nan
}

RangeTraps.test("nanLowerBoundPartial")
.code {
expectCrashLater()
_ = Double.nan ..< 0
}

RangeTraps.test("nanUpperBoundPartial")
.code {
expectCrashLater()
_ = 0 ..< Double.nan
}

RangeTraps.test("fromNaN")
.code {
expectCrashLater()
_ = Double.nan...
}

RangeTraps.test("toNaN")
.code {
expectCrashLater()
_ = ..<Double.nan
}

RangeTraps.test("throughNaN")
.code {
expectCrashLater()
_ = ...Double.nan
}

runAllTests()

2 changes: 1 addition & 1 deletion validation-test/stdlib/Range.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ ${TestSuite}.test("Equatable") {

${TestSuite}.test("'${op}' traps when upperBound < lowerBound")
.crashOutputMatches(_isDebugAssertConfiguration() ?
"Can't form Range with upperBound < lowerBound" : "")
"Range requires lowerBound <= upperBound" : "")
.code {
let _1 = ${Bound}(1)
let _2 = ${Bound}(2)
Expand Down