Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes redundant buffer zeroing in JSONSerialization #2703

Merged
merged 2 commits into from
Mar 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Sources/Foundation/JSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ open class JSONSerialization : NSObject {
fatalError("Stream is not available for reading")
}
repeat {
var buffer = [UInt8](repeating: 0, count: 1024)
var bytesRead: Int = 0
bytesRead = stream.read(&buffer, maxLength: buffer.count)
if bytesRead < 0 {
throw stream.streamError!
} else {
data.append(&buffer, count: bytesRead)
let buffer = try [UInt8](unsafeUninitializedCapacity: 1024) { buf, initializedCount in
let bytesRead = stream.read(buf.baseAddress!, maxLength: buf.count)
initializedCount = bytesRead
guard bytesRead >= 0 else {
throw stream.streamError!
}
}
data.append(buffer, count: buffer.count)
} while stream.hasBytesAvailable
return try jsonObject(with: data, options: opt)
}
Expand Down