Skip to content

Commit

Permalink
fixing the dangling pointers. PerfectlySoft#54
Browse files Browse the repository at this point in the history
  • Loading branch information
SkOODaT committed Mar 21, 2022
1 parent 6a09a6c commit a418aaf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
13 changes: 10 additions & 3 deletions Sources/PerfectHTTPServer/HTTP11/HTTP11Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,11 @@ class HTTP11Request: HTTPRequest {
workingBuffer.removeAll()
case .headerField:
workingBuffer.append(0)
lastHeaderName = String(validatingUTF8: UnsafeMutableRawPointer(mutating: workingBuffer).assumingMemoryBound(to: Int8.self))
lastHeaderName = workingBuffer.withUnsafeBufferPointer {
return $0.baseAddress?.withMemoryRebound(to: Int8.self, capacity: workingBuffer.count) {
return String(validatingUTF8: $0)
}
}
workingBuffer.removeAll()
case .headerValue:
if let name = lastHeaderName {
Expand Down Expand Up @@ -476,8 +480,11 @@ class HTTP11Request: HTTPRequest {
// false indicates that the request either was fully read and is being processed or that the request failed
// either way no further action should be taken
func didReadSomeBytes(_ b: [UInt8], callback: @escaping StatusCallback) -> Bool {
_ = UnsafePointer(b).withMemoryRebound(to: Int8.self, capacity: b.count) {
http_parser_execute(&parser, &parserSettings, $0, b.count)
var mutableB = b
_ = mutableB.withUnsafeMutableBufferPointer { buffered in
buffered.baseAddress?.withMemoryRebound(to: Int8.self, capacity: b.count) {
http_parser_execute(&parser, &parserSettings, $0, b.count)
}
}

let http_errno = parser.http_errno
Expand Down
7 changes: 6 additions & 1 deletion Sources/PerfectHTTPServer/HTTPContentCompression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ class ZlibStream {
dest.deallocate()
}
if !bytes.isEmpty {
stream.next_in = UnsafeMutablePointer(mutating: bytes)
var byts = bytes
byts.withUnsafeMutableBufferPointer { buffered in
buffered.baseAddress?.withMemoryRebound(to: Bytef.self, capacity: bytes.count) {
stream.next_in = $0
}
}
stream.avail_in = uInt(bytes.count)
} else {
stream.next_in = nil
Expand Down

0 comments on commit a418aaf

Please sign in to comment.