Skip to content

On targets smaller than 64 bits, emit a compile-time error if withUnsafeTemporaryAllocation(of:capacity:) byte count overflows #39976

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
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
8 changes: 8 additions & 0 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3026,6 +3026,14 @@ static llvm::Value *getStackAllocationSize(IRGenSILFunction &IGF,
auto overflow = llvm::MulOverflow(*capacityValue, *strideValue, byteCount);
if (overflow) {
Diags.diagnose(loc, diag::temporary_allocation_size_overflow);
} else {
// For architectures narrower than 64 bits, check if the byte count fits
// in a (signed) size value.
auto maxByteCount = llvm::APInt::getSignedMaxValue(
IGF.IGM.SizeTy->getBitWidth()).getSExtValue();
if (byteCount > maxByteCount) {
Diags.diagnose(loc, diag::temporary_allocation_size_overflow);
}
}
result = llvm::ConstantInt::get(IGF.IGM.SizeTy, byteCount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@_silgen_name("blackHole")
func blackHole(_ value: UnsafeMutableRawPointer?) -> Void

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