Skip to content

Commit 018868e

Browse files
committed
[Linux] Don't keep the file descriptor for FileImageSource.
We don't really need it after calling `mmap()`, and not keeping it around means it's easy to make sure it gets closed properly in all cases. rdar://117590155
1 parent 8678ead commit 018868e

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

stdlib/public/Backtracing/FileImageSource.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ enum FileImageSourceError: Error {
2424
}
2525

2626
class FileImageSource: ImageSource {
27-
private var _fd: Int32
2827
private var _mapping: UnsafeRawBufferPointer
2928

3029
public var isMappedImage: Bool { return false }
@@ -38,15 +37,16 @@ class FileImageSource: ImageSource {
3837

3938
public init(path: String) throws {
4039
_path = path
41-
_fd = _swift_open(path, O_RDONLY, 0)
42-
if _fd < 0 {
40+
let fd = _swift_open(path, O_RDONLY, 0)
41+
if fd < 0 {
4342
throw FileImageSourceError.posixError(_swift_get_errno())
4443
}
45-
let size = lseek(_fd, 0, SEEK_END)
44+
defer { close(fd) }
45+
let size = lseek(fd, 0, SEEK_END)
4646
if size < 0 {
4747
throw FileImageSourceError.posixError(_swift_get_errno())
4848
}
49-
let base = mmap(nil, Int(size), PROT_READ, MAP_FILE|MAP_PRIVATE, _fd, 0)
49+
let base = mmap(nil, Int(size), PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0)
5050
if base == nil || base! == UnsafeRawPointer(bitPattern: -1)! {
5151
throw FileImageSourceError.posixError(_swift_get_errno())
5252
}
@@ -56,7 +56,6 @@ class FileImageSource: ImageSource {
5656
deinit {
5757
munmap(UnsafeMutableRawPointer(mutating: _mapping.baseAddress),
5858
_mapping.count)
59-
close(_fd)
6059
}
6160

6261
public func fetch<T>(from addr: Address,

0 commit comments

Comments
 (0)