Skip to content

Commit 330085e

Browse files
committed
[Linux] Improve the bounds checks.
It was pointed out that there was a more Swift-y way to write the bounds checks for the `fetch<T>` implementation, so do that instead. rdar://117590155
1 parent 018868e commit 330085e

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

stdlib/public/Backtracing/FileImageSource.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ class FileImageSource: ImageSource {
6060

6161
public func fetch<T>(from addr: Address,
6262
into buffer: UnsafeMutableBufferPointer<T>) throws {
63-
let size = buffer.count * MemoryLayout<T>.stride
64-
guard Int(addr) < _mapping.count && _mapping.count - Int(addr) >= size else {
65-
throw FileImageSourceError.outOfRangeRead
66-
}
67-
let slice = _mapping[Int(addr)...]
68-
_ = buffer.withMemoryRebound(to: UInt8.self) { byteBuf in
63+
let start = Int(addr)
64+
_ = try buffer.withMemoryRebound(to: UInt8.self) { byteBuf in
65+
guard _mapping.indices.contains(start) else {
66+
throw FileImageSourceError.outOfRangeRead
67+
}
68+
let slice = _mapping[start...]
69+
guard slice.count >= byteBuf.count else {
70+
throw FileImageSourceError.outOfRangeRead
71+
}
6972
byteBuf.update(from: slice)
7073
}
7174
}

0 commit comments

Comments
 (0)