Skip to content

Commit cd1e20d

Browse files
committed
Android: add better nullability checks for nullability annotations added in NDK 26
1 parent 9f011c6 commit cd1e20d

File tree

6 files changed

+38
-17
lines changed

6 files changed

+38
-17
lines changed

Sources/Foundation/FileHandle.swift

+6
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,15 @@ open class FileHandle : NSObject {
310310
let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0)
311311
// Swift does not currently expose MAP_FAILURE
312312
if data != UnsafeMutableRawPointer(bitPattern: -1) {
313+
#if os(Android)
314+
return NSData.NSDataReadResult(bytes: data, length: mapSize) { buffer, length in
315+
munmap(buffer, length)
316+
}
317+
#else
313318
return NSData.NSDataReadResult(bytes: data!, length: mapSize) { buffer, length in
314319
munmap(buffer, length)
315320
}
321+
#endif
316322
}
317323
}
318324

Sources/Foundation/FileManager+POSIX.swift

+24-10
Original file line numberDiff line numberDiff line change
@@ -741,32 +741,38 @@ extension FileManager {
741741
if rmdir(fsRep) == 0 {
742742
return
743743
} else if errno == ENOTEMPTY {
744+
#if os(Android)
745+
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>>.allocate(capacity: 2)
746+
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
747+
ps.advanced(by: 1).initialize(to: unsafeBitCast(0, to: UnsafeMutablePointer<Int8>.self))
748+
#else
744749
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
745750
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
746751
ps.advanced(by: 1).initialize(to: nil)
752+
#endif
747753
let stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
748754
ps.deinitialize(count: 2)
749755
ps.deallocate()
750756

751-
if stream != nil {
757+
if let openStream = stream {
752758
defer {
753-
fts_close(stream)
759+
fts_close(openStream)
754760
}
755761

756-
while let current = fts_read(stream)?.pointee {
757-
let itemPath = string(withFileSystemRepresentation: current.fts_path, length: Int(current.fts_pathlen))
762+
while let current = fts_read(openStream)?.pointee, let current_path = current.fts_path {
763+
let itemPath = string(withFileSystemRepresentation: current_path, length: Int(current.fts_pathlen))
758764
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
759765
continue
760766
}
761767

762768
do {
763769
switch Int32(current.fts_info) {
764770
case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE:
765-
if unlink(current.fts_path) == -1 {
771+
if unlink(current_path) == -1 {
766772
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
767773
}
768774
case FTS_DP:
769-
if rmdir(current.fts_path) == -1 {
775+
if rmdir(current_path) == -1 {
770776
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
771777
}
772778
case FTS_DNR, FTS_ERR, FTS_NS:
@@ -1085,10 +1091,18 @@ extension FileManager {
10851091
do {
10861092
guard fm.fileExists(atPath: _url.path) else { throw _NSErrorWithErrno(ENOENT, reading: true, url: url) }
10871093
_stream = try FileManager.default._fileSystemRepresentation(withPath: _url.path) { fsRep in
1094+
#if os(Android)
1095+
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>>.allocate(capacity: 2)
1096+
#else
10881097
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
1098+
#endif
10891099
defer { ps.deallocate() }
10901100
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
1101+
#if os(Android)
1102+
ps.advanced(by: 1).initialize(to: unsafeBitCast(0, to: UnsafeMutablePointer<Int8>.self))
1103+
#else
10911104
ps.advanced(by: 1).initialize(to: nil)
1105+
#endif
10921106
return fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
10931107
}
10941108
if _stream == nil {
@@ -1135,14 +1149,14 @@ extension FileManager {
11351149
}
11361150

11371151
_current = fts_read(stream)
1138-
while let current = _current {
1139-
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path, length: Int(current.pointee.fts_pathlen))
1152+
while let current = _current, let current_path = current.pointee.fts_path {
1153+
let filename = FileManager.default.string(withFileSystemRepresentation: current_path, length: Int(current.pointee.fts_pathlen))
11401154

11411155
switch Int32(current.pointee.fts_info) {
11421156
case FTS_D:
11431157
let (showFile, skipDescendants) = match(filename: filename, to: _options, isDir: true)
11441158
if skipDescendants {
1145-
fts_set(_stream, _current, FTS_SKIP)
1159+
fts_set(stream, current, FTS_SKIP)
11461160
}
11471161
if showFile {
11481162
return URL(fileURLWithPath: filename, isDirectory: true)
@@ -1315,7 +1329,7 @@ extension FileManager {
13151329
let finalErrno = originalItemURL.withUnsafeFileSystemRepresentation { (originalFS) -> Int32? in
13161330
return newItemURL.withUnsafeFileSystemRepresentation { (newItemFS) -> Int32? in
13171331
// This is an atomic operation in many OSes, but is not guaranteed to be atomic by the standard.
1318-
if rename(newItemFS, originalFS) == 0 {
1332+
if let newFS = newItemFS, let origFS = originalFS, rename(newFS, origFS) == 0 {
13191333
return nil
13201334
} else {
13211335
return errno

Sources/Foundation/FileManager.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,13 @@ open class FileManager : NSObject {
568568
let attributes = try windowsFileAttributes(atPath: path)
569569
let type = FileAttributeType(attributes: attributes, atPath: path)
570570
#else
571-
if let pwd = getpwuid(s.st_uid), pwd.pointee.pw_name != nil {
572-
let name = String(cString: pwd.pointee.pw_name)
571+
if let pwd = getpwuid(s.st_uid), let pwd_name = pwd.pointee.pw_name {
572+
let name = String(cString: pwd_name)
573573
result[.ownerAccountName] = name
574574
}
575575

576-
if let grd = getgrgid(s.st_gid), grd.pointee.gr_name != nil {
577-
let name = String(cString: grd.pointee.gr_name)
576+
if let grd = getgrgid(s.st_gid), let grd_name = grd.pointee.gr_name {
577+
let name = String(cString: grd_name)
578578
result[.groupOwnerAccountName] = name
579579
}
580580

Sources/Foundation/Host.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import WinSDK
2525

2626
// getnameinfo uses size_t for its 4th and 6th arguments.
2727
private func getnameinfo(_ addr: UnsafePointer<sockaddr>?, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
28-
return Glibc.getnameinfo(addr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
28+
guard let saddr = addr else { return -1 }
29+
return Glibc.getnameinfo(saddr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
2930
}
3031

3132
// getifaddrs and freeifaddrs are not available in Android 6.0 or earlier, so call these functions dynamically.

Tests/Foundation/Tests/TestFileHandle.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class TestFileHandle : XCTestCase {
111111
#else
112112
var fds: [Int32] = [-1, -1]
113113
fds.withUnsafeMutableBufferPointer { (pointer) -> Void in
114-
pipe(pointer.baseAddress)
114+
pipe(pointer.baseAddress!)
115115
}
116116

117117
close(fds[1])

Tests/Foundation/Tests/TestTimeZone.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class TestTimeZone: XCTestCase {
160160
var lt = tm()
161161
localtime_r(&t, &lt)
162162
let zoneName = NSTimeZone.system.abbreviation() ?? "Invalid Abbreviation"
163-
let expectedName = String(cString: lt.tm_zone, encoding: .ascii) ?? "Invalid Zone"
163+
let expectedName = String(cString: lt.tm_zone!, encoding: .ascii) ?? "Invalid Zone"
164164
XCTAssertEqual(zoneName, expectedName, "expected name \"\(expectedName)\" is not equal to \"\(zoneName)\"")
165165
}
166166
#endif

0 commit comments

Comments
 (0)