Skip to content

Commit 3ab0ae2

Browse files
committed
On targets smaller than 64 bits, emit a compile-time error if withUnsafeTemporaryAllocation(of:capacity:) byte count overflows
1 parent 94d824a commit 3ab0ae2

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/IRGen/IRGenSIL.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3026,6 +3026,14 @@ static llvm::Value *getStackAllocationSize(IRGenSILFunction &IGF,
30263026
auto overflow = llvm::MulOverflow(*capacityValue, *strideValue, byteCount);
30273027
if (overflow) {
30283028
Diags.diagnose(loc, diag::temporary_allocation_size_overflow);
3029+
} else {
3030+
// For architectures narrower than 64 bits, check if the byte count fits
3031+
// in a (signed) size value.
3032+
auto maxByteCount = llvm::APInt::getSignedMaxValue(
3033+
IGF.IGM.SizeTy->getBitWidth()).getSExtValue();
3034+
if (byteCount > maxByteCount) {
3035+
Diags.diagnose(loc, diag::temporary_allocation_size_overflow);
3036+
}
30293037
}
30303038
result = llvm::ConstantInt::get(IGF.IGM.SizeTy, byteCount);
30313039

test/IRGen/temporary_allocation/bad_constants.swift renamed to test/IRGen/temporary_allocation/size_too_large.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@_silgen_name("blackHole")
44
func blackHole(_ value: UnsafeMutableRawPointer?) -> Void
55

6-
withUnsafeTemporaryAllocation(byteCount: 1, alignment: -1) { buffer in
6+
withUnsafeTemporaryAllocation(of: Int.self, capacity: .max) { buffer in
77
blackHole(buffer.baseAddress)
88
}
9-
// CHECK: error: alignment value must be greater than zero
9+
// CHECK: error: allocation byte count too large

0 commit comments

Comments
 (0)