Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Commit

Permalink
Make sure modification accesses are exclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuhaow committed May 10, 2018
1 parent 612aa91 commit 065be82
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
21 changes: 10 additions & 11 deletions src/Socket/AdapterSocket/Shadowsocks/ProtocolObfuscater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,16 @@ extension ShadowsocksAdapter {
var unpackedData = Data()
while buffer.left > 5 {
buffer.skip(3)
if !buffer.withUnsafeBytes({ (ptr: UnsafePointer<UInt16>) -> Bool in
let length = Int(ptr.pointee.byteSwapped)
self.buffer.skip(2)
if self.buffer.left >= length {
unpackedData.append(self.buffer.get(length: length)!)
return true
} else {
self.buffer.setBack(length: 5)
return false
}
}) {
var length: Int = 0
buffer.withUnsafeBytes({ (ptr: UnsafePointer<UInt16>) in
length = Int(ptr.pointee.byteSwapped)
})
buffer.skip(2)
if buffer.left >= length {
unpackedData.append(buffer.get(length: length)!)
continue
} else {
buffer.setBack(length: 5)
break
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Utils/Buffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ struct Buffer {
return
}

let buffer_ = buffer
buffer.withUnsafeMutableBytes {
buffer.copyBytes(to: $0, from: offset..<buffer.count)
buffer_.copyBytes(to: $0, from: offset..<buffer_.count)
}
buffer.replaceSubrange(buffer.count - offset ..< buffer.count, with: Data())
offset = 0
Expand Down

0 comments on commit 065be82

Please sign in to comment.