diff --git a/Sources/Foundation/FileHandle.swift b/Sources/Foundation/FileHandle.swift index 72ab09a3f6..af0bfa40eb 100644 --- a/Sources/Foundation/FileHandle.swift +++ b/Sources/Foundation/FileHandle.swift @@ -321,10 +321,15 @@ open class FileHandle : NSObject { if options.contains(.alwaysMapped) { // Filesizes are often 64bit even on 32bit systems let mapSize = min(length, Int(clamping: statbuf.st_size)) + #if canImport(Android) + // Bionic mmap() now returns _Nonnull, so the force unwrap below isn't needed. let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0) + #else + let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0)! + #endif // Swift does not currently expose MAP_FAILURE if data != UnsafeMutableRawPointer(bitPattern: -1) { - return NSData.NSDataReadResult(bytes: data!, length: mapSize) { buffer, length in + return NSData.NSDataReadResult(bytes: data, length: mapSize) { buffer, length in munmap(buffer, length) } } diff --git a/Sources/Foundation/FileManager+POSIX.swift b/Sources/Foundation/FileManager+POSIX.swift index 73d8171832..9ec12c680a 100644 --- a/Sources/Foundation/FileManager+POSIX.swift +++ b/Sources/Foundation/FileManager+POSIX.swift @@ -812,8 +812,8 @@ extension FileManager { fts_close(stream) } - while let current = fts_read(stream)?.pointee { - let itemPath = string(withFileSystemRepresentation: current.fts_path, length: Int(current.fts_pathlen)) + while let current = fts_read(stream)?.pointee, let current_path = current.fts_path { + let itemPath = string(withFileSystemRepresentation: current_path, length: Int(current.fts_pathlen)) guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else { continue } @@ -821,11 +821,11 @@ extension FileManager { do { switch Int32(current.fts_info) { case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE: - if unlink(current.fts_path) == -1 { + if unlink(current_path) == -1 { throw _NSErrorWithErrno(errno, reading: false, path: itemPath) } case FTS_DP: - if rmdir(current.fts_path) == -1 { + if rmdir(current_path) == -1 { throw _NSErrorWithErrno(errno, reading: false, path: itemPath) } case FTS_DNR, FTS_ERR, FTS_NS: @@ -1217,14 +1217,14 @@ extension FileManager { } _current = fts_read(stream) - while let current = _current { - let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path, length: Int(current.pointee.fts_pathlen)) + while let current = _current, let current_path = current.pointee.fts_path { + let filename = FileManager.default.string(withFileSystemRepresentation: current_path, length: Int(current.pointee.fts_pathlen)) switch Int32(current.pointee.fts_info) { case FTS_D: let (showFile, skipDescendants) = match(filename: filename, to: _options, isDir: true) if skipDescendants { - fts_set(_stream, _current, FTS_SKIP) + fts_set(stream, current, FTS_SKIP) } if showFile { return URL(fileURLWithPath: filename, isDirectory: true) diff --git a/Sources/Foundation/FileManager.swift b/Sources/Foundation/FileManager.swift index fdd8411511..a3e9146f9b 100644 --- a/Sources/Foundation/FileManager.swift +++ b/Sources/Foundation/FileManager.swift @@ -579,13 +579,13 @@ open class FileManager : NSObject { #elseif os(WASI) let type = FileAttributeType(statMode: mode_t(s.st_mode)) #else - if let pwd = getpwuid(s.st_uid), pwd.pointee.pw_name != nil { - let name = String(cString: pwd.pointee.pw_name) + if let pwd = getpwuid(s.st_uid), let pwd_name = pwd.pointee.pw_name { + let name = String(cString: pwd_name) result[.ownerAccountName] = name } - if let grd = getgrgid(s.st_gid), grd.pointee.gr_name != nil { - let name = String(cString: grd.pointee.gr_name) + if let grd = getgrgid(s.st_gid), let grd_name = grd.pointee.gr_name { + let name = String(cString: grd_name) result[.groupOwnerAccountName] = name } diff --git a/Tests/Foundation/Tests/TestFileHandle.swift b/Tests/Foundation/Tests/TestFileHandle.swift index 5416c41c4e..91e1bdc0ac 100644 --- a/Tests/Foundation/Tests/TestFileHandle.swift +++ b/Tests/Foundation/Tests/TestFileHandle.swift @@ -111,7 +111,7 @@ class TestFileHandle : XCTestCase { #else var fds: [Int32] = [-1, -1] fds.withUnsafeMutableBufferPointer { (pointer) -> Void in - pipe(pointer.baseAddress) + pipe(pointer.baseAddress!) } close(fds[1]) diff --git a/Tests/Foundation/Tests/TestTimeZone.swift b/Tests/Foundation/Tests/TestTimeZone.swift index 5e15c00026..373e21b263 100644 --- a/Tests/Foundation/Tests/TestTimeZone.swift +++ b/Tests/Foundation/Tests/TestTimeZone.swift @@ -160,7 +160,7 @@ class TestTimeZone: XCTestCase { var lt = tm() localtime_r(&t, <) let zoneName = NSTimeZone.system.abbreviation() ?? "Invalid Abbreviation" - let expectedName = String(cString: lt.tm_zone, encoding: .ascii) ?? "Invalid Zone" + let expectedName = String(cString: lt.tm_zone!, encoding: .ascii) ?? "Invalid Zone" XCTAssertEqual(zoneName, expectedName, "expected name \"\(expectedName)\" is not equal to \"\(zoneName)\"") } #endif