Skip to content

Commit 8cd9bc5

Browse files
committed
Fix a race condition
In the case that the remote token responds too quickly, the response will be treated as output data and sent back to the token. We resolve this race condition by clearly demarcating input and output chunks as separate variables.
1 parent bc1fd09 commit 8cd9bc5

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

u2f-ble-test-ios/Managers/DeviceManager.swift

+8-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ final class DeviceManager: NSObject {
2929
var onAPDUReceived: ((DeviceManager, Data) -> Void)?
3030

3131
fileprivate var chunksize = 0
32-
fileprivate var pendingChunks: [Data] = []
32+
fileprivate var pendingOutput: [Data] = []
33+
fileprivate var pendingInput: [Data] = []
3334
fileprivate var writeCharacteristic: CBCharacteristic?
3435
fileprivate var notifyCharacteristic: CBCharacteristic?
3536
fileprivate var controlpointLengthCharacteristic: CBCharacteristic?
@@ -68,7 +69,7 @@ final class DeviceManager: NSObject {
6869
onDebugMessage?(self, "Trying to split APDU into chunks...")
6970
if let chunks = TransportHelper.split(data, command: .message, chuncksize: chunksize), chunks.count > 0 {
7071
onDebugMessage?(self, "Successfully split APDU into \(chunks.count) part(s)")
71-
pendingChunks = chunks
72+
pendingOutput = chunks
7273
writeNextPendingChunk()
7374
}
7475
else {
@@ -78,12 +79,12 @@ final class DeviceManager: NSObject {
7879
}
7980

8081
fileprivate func writeNextPendingChunk() {
81-
guard pendingChunks.count > 0 else {
82+
guard pendingOutput.count > 0 else {
8283
onDebugMessage?(self, "Trying to write pending chunk but nothing left to write")
8384
return
8485
}
8586

86-
let chunk = pendingChunks.removeFirst()
87+
let chunk = pendingOutput.removeFirst()
8788
onDebugMessage?(self, "Writing pending chunk = \(chunk)")
8889
peripheral.writeValue(chunk, for: writeCharacteristic!, type: .withResponse)
8990
}
@@ -109,10 +110,10 @@ final class DeviceManager: NSObject {
109110
}
110111

111112
// join APDU
112-
pendingChunks.append(chunk)
113-
if let APDU = TransportHelper.join(pendingChunks, command: .message) {
113+
pendingInput.append(chunk)
114+
if let APDU = TransportHelper.join(pendingInput, command: .message) {
114115
onDebugMessage?(self, "Successfully joined APDU = \(APDU)")
115-
pendingChunks.removeAll()
116+
pendingInput.removeAll()
116117
onAPDUReceived?(self, APDU)
117118
}
118119
}

0 commit comments

Comments
 (0)