Skip to content

Commit 4b8f6a2

Browse files
glbrnttfabianfett
authored andcommitted
Change 'unsafeDowncast' to 'as!' (#495)
Motivation: The 'unsafeDowncast' can cause a miscompile leading to unexpected runtime behaviour. Modifications: - Use 'as!' instead Result: No miscompiles on 5.10
1 parent 5c26876 commit 4b8f6a2

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

Sources/ConnectionPoolModule/NIOLock.swift

+15-14
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ extension LockOperations {
5252
debugOnly {
5353
pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK))
5454
}
55-
55+
5656
let err = pthread_mutex_init(mutex, &attr)
5757
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
5858
#endif
5959
}
60-
60+
6161
@inlinable
6262
static func destroy(_ mutex: UnsafeMutablePointer<LockPrimitive>) {
6363
mutex.assertValidAlignment()
@@ -69,7 +69,7 @@ extension LockOperations {
6969
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
7070
#endif
7171
}
72-
72+
7373
@inlinable
7474
static func lock(_ mutex: UnsafeMutablePointer<LockPrimitive>) {
7575
mutex.assertValidAlignment()
@@ -81,7 +81,7 @@ extension LockOperations {
8181
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
8282
#endif
8383
}
84-
84+
8585
@inlinable
8686
static func unlock(_ mutex: UnsafeMutablePointer<LockPrimitive>) {
8787
mutex.assertValidAlignment()
@@ -125,49 +125,50 @@ extension LockOperations {
125125
// See also: https://github.com/apple/swift/pull/40000
126126
@usableFromInline
127127
final class LockStorage<Value>: ManagedBuffer<Value, LockPrimitive> {
128-
128+
129129
@inlinable
130130
static func create(value: Value) -> Self {
131131
let buffer = Self.create(minimumCapacity: 1) { _ in
132132
return value
133133
}
134-
let storage = unsafeDowncast(buffer, to: Self.self)
135-
134+
// Avoid 'unsafeDowncast' as there is a miscompilation on 5.10.
135+
let storage = buffer as! Self
136+
136137
storage.withUnsafeMutablePointers { _, lockPtr in
137138
LockOperations.create(lockPtr)
138139
}
139-
140+
140141
return storage
141142
}
142-
143+
143144
@inlinable
144145
func lock() {
145146
self.withUnsafeMutablePointerToElements { lockPtr in
146147
LockOperations.lock(lockPtr)
147148
}
148149
}
149-
150+
150151
@inlinable
151152
func unlock() {
152153
self.withUnsafeMutablePointerToElements { lockPtr in
153154
LockOperations.unlock(lockPtr)
154155
}
155156
}
156-
157+
157158
@inlinable
158159
deinit {
159160
self.withUnsafeMutablePointerToElements { lockPtr in
160161
LockOperations.destroy(lockPtr)
161162
}
162163
}
163-
164+
164165
@inlinable
165166
func withLockPrimitive<T>(_ body: (UnsafeMutablePointer<LockPrimitive>) throws -> T) rethrows -> T {
166167
try self.withUnsafeMutablePointerToElements { lockPtr in
167168
return try body(lockPtr)
168169
}
169170
}
170-
171+
171172
@inlinable
172173
func withLockedValue<T>(_ mutate: (inout Value) throws -> T) rethrows -> T {
173174
try self.withUnsafeMutablePointers { valuePtr, lockPtr in
@@ -192,7 +193,7 @@ extension LockStorage: @unchecked Sendable { }
192193
struct NIOLock {
193194
@usableFromInline
194195
internal let _storage: LockStorage<Void>
195-
196+
196197
/// Create a new lock.
197198
@inlinable
198199
init() {

0 commit comments

Comments
 (0)