Skip to content

Commit

Permalink
Add uncoveredBug test case for #73 (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed May 6, 2024
1 parent 48ad532 commit 4720fcf
Showing 1 changed file with 26 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,7 @@ struct PointerOffsetTests {
#expect(pointer[offset: firstOffset] == 3)
#expect(pointer[offset: secondOffset].isApproximatelyEqual(to: 4.0))
}
#if !canImport(Darwin) && !DEBUG
// FIXME: The issue only occur on Linux + Release configuration (Swift 5.10)

// Uncomment the following withKnownIssue code will make the result back to normal thus causing 5 new issues
// withKnownIssue {
// withUnsafePointer(to: tuple) { pointer in
// #expect(pointer[offset: firstOffset] == 3)
// #expect(pointer[offset: secondOffset].isApproximatelyEqual(to: 4.0))
// }
// #expect(tuple.first == 3)
// #expect(tuple.second.isApproximatelyEqual(to: 4.0))
// }
withUnsafePointer(to: tuple) { pointer in
#expect(pointer[offset: firstOffset] == 1)
#expect(pointer[offset: secondOffset].isApproximatelyEqual(to: 2.0))
}
#expect(tuple.first == 1)
#expect(tuple.second.isApproximatelyEqual(to: 2.0))
#else
#if !(!canImport(Darwin) && !DEBUG)
withUnsafePointer(to: tuple) { pointer in
#expect(pointer[offset: firstOffset] == 3)
#expect(pointer[offset: secondOffset].isApproximatelyEqual(to: 4.0))
Expand Down Expand Up @@ -163,29 +145,7 @@ struct PointerOffsetTests {
#expect((pointer + secondOffset).pointee == 4)
#expect((pointer + thirdOffset).pointee == 5)
}
#if !canImport(Darwin) && !DEBUG
// FIXME: The issue only occur on Linux + Release configuration (Swift 5.10)

// Uncomment the following withKnownIssue code will make the result back to normal thus causing 7 new issues
// withKnownIssue {
// withUnsafePointer(to: triple) { pointer in
// #expect((pointer + firstOffset).pointee == 3)
// #expect((pointer + secondOffset).pointee == 4)
// #expect((pointer + thirdOffset).pointee == 5)
// }
// #expect(triple.first == 3)
// #expect(triple.second == 4)
// #expect(triple.third == 5)
// }
withUnsafePointer(to: triple) { pointer in
#expect((pointer + firstOffset).pointee == 0)
#expect((pointer + secondOffset).pointee == 1)
#expect((pointer + thirdOffset).pointee == 2)
}
#expect(triple.first == 0)
#expect(triple.second == 1)
#expect(triple.third == 2)
#else
#if !(!canImport(Darwin) && !DEBUG)
withUnsafePointer(to: triple) { pointer in
#expect((pointer + firstOffset).pointee == 3)
#expect((pointer + secondOffset).pointee == 4)
Expand All @@ -197,4 +157,28 @@ struct PointerOffsetTests {
#endif
}
}

#if !canImport(Darwin) && !DEBUG
@Test("Undefined Behavior Issue for PointerOffset", .bug("#73", relationship: .uncoveredBug))
func unsafePointerAndUnsafeMutablePointerExtensionUB() {
var tuple = Tuple(first: 1, second: 2)
typealias Base = Tuple<Int, Int>
let firstOffset = PointerOffset<Base, Int>(byteOffset: 0)
withUnsafeMutablePointer(to: &tuple) { pointer in
#expect(pointer[offset: firstOffset] == 1)
pointer[offset: firstOffset] = 3
}
let unexpectedValue = tuple.first
// The value will be unexpected due to UB
#expect(unexpectedValue == 1)
// The value will be expected after and within a withKnownIssue block
withKnownIssue {
let expectedValue = tuple.first
#expect(expectedValue == 3)
Issue.record("To make withKnownIssue pass")
}
let expectedValue = tuple.first
#expect(expectedValue == 3)
}
#endif
}

0 comments on commit 4720fcf

Please sign in to comment.