Skip to content

Commit 56c6887

Browse files
committed
[test] test overflow preconditions in collection copies
1 parent ffa7b0e commit 56c6887

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

validation-test/stdlib/UnsafeBufferPointer.swift.gyb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,4 +1193,101 @@ UnsafeRawBufferPointerTestSuite.test("assumingMemoryBound") {
11931193
expectEqual(mutableArray[3], array[3]+1)
11941194
}
11951195

1196+
UnsafeMutableBufferPointerTestSuite.test("UMBP.initialize.collection.overflow")
1197+
.skip(.custom({
1198+
if #available(SwiftStdlib 5.8, *) { return false } else { return true }
1199+
}, reason: "Requires standard library from Swift 5.8"))
1200+
.code {
1201+
let s = Array<Int>(0..<8)
1202+
let b = UnsafeMutableBufferPointer<Int>.allocate(capacity: s.count-1)
1203+
defer { b.deallocate() }
1204+
expectCrash {
1205+
_ = b.initialize(fromContentsOf: s)
1206+
}
1207+
}
1208+
1209+
UnsafeMutableBufferPointerTestSuite.test("UMBP.update.collection.overflow")
1210+
.skip(.custom({
1211+
if #available(SwiftStdlib 5.8, *) { return false } else { return true }
1212+
}, reason: "Requires standard library from Swift 5.8"))
1213+
.code {
1214+
let s = Array(0..<8)
1215+
var b = Array(repeating: 0, count: s.count-1)
1216+
b.withUnsafeMutableBufferPointer { b in
1217+
expectCrash {
1218+
_ = b.update(fromContentsOf: s)
1219+
}
1220+
}
1221+
}
1222+
1223+
UnsafeMutableBufferPointerTestSuite.test("UMBP.moveInitialize.overflow")
1224+
.skip(.custom({
1225+
if #available(SwiftStdlib 5.8, *) { return false } else { return true }
1226+
}, reason: "Requires standard library from Swift 5.8"))
1227+
.code {
1228+
let s = UnsafeMutableBufferPointer<Int>.allocate(capacity: 8)
1229+
defer { s.deallocate() }
1230+
s.initialize(fromContentsOf: 0..<s.count)
1231+
1232+
let b = UnsafeMutableBufferPointer<Int>.allocate(capacity: s.count-1)
1233+
defer { b.deallocate() }
1234+
expectCrash {
1235+
_ = b.moveInitialize(fromContentsOf: s)
1236+
}
1237+
}
1238+
1239+
UnsafeMutableBufferPointerTestSuite.test("UMBP.moveUpdate.overflow")
1240+
.skip(.custom({
1241+
if #available(SwiftStdlib 5.8, *) { return false } else { return true }
1242+
}, reason: "Requires standard library from Swift 5.8"))
1243+
.code {
1244+
let s = UnsafeMutableBufferPointer<Int>.allocate(capacity: 8)
1245+
defer { s.deallocate() }
1246+
s.initialize(fromContentsOf: 0..<s.count)
1247+
1248+
var b = Array(repeating: 0, count: s.count-1)
1249+
b.withUnsafeMutableBufferPointer { b in
1250+
expectCrash {
1251+
_ = b.moveUpdate(fromContentsOf: s)
1252+
}
1253+
}
1254+
}
1255+
1256+
UnsafeMutableRawBufferPointerTestSuite.test("UMRBP.initializeMemory.collection.overflow")
1257+
.skip(.custom({
1258+
if #available(SwiftStdlib 5.8, *) { return false } else { return true }
1259+
}, reason: "Requires standard library from Swift 5.8"))
1260+
.code {
1261+
let s = Array<Int>(0..<8)
1262+
let b = UnsafeMutableRawBufferPointer.allocate(
1263+
byteCount: MemoryLayout<Int>.stride * (s.count-1),
1264+
alignment: MemoryLayout<Int>.alignment
1265+
)
1266+
defer { b.deallocate() }
1267+
expectCrash {
1268+
let i = b.initializeMemory(as: Int.self, fromContentsOf: s)
1269+
i.deinitialize()
1270+
}
1271+
}
1272+
1273+
UnsafeMutableRawBufferPointerTestSuite.test("UMRBP.moveInitializeMemory.overflow")
1274+
.skip(.custom({
1275+
if #available(SwiftStdlib 5.8, *) { return false } else { return true }
1276+
}, reason: "Requires standard library from Swift 5.8"))
1277+
.code {
1278+
let s = UnsafeMutableBufferPointer<Int>.allocate(capacity: 8)
1279+
defer { s.deallocate() }
1280+
s.initialize(fromContentsOf: 0..<s.count)
1281+
1282+
let b = UnsafeMutableRawBufferPointer.allocate(
1283+
byteCount: MemoryLayout<Int>.stride * (s.count-1),
1284+
alignment: MemoryLayout<Int>.alignment
1285+
)
1286+
defer { b.deallocate() }
1287+
expectCrash {
1288+
let i = b.moveInitializeMemory(as: Int.self, fromContentsOf: s)
1289+
i.deinitialize()
1290+
}
1291+
}
1292+
11961293
runAllTests()

0 commit comments

Comments
 (0)