Skip to content

Commit 18b26df

Browse files
Lukasaweissi
authored andcommitted
Avoid using un-thread-safe static buffers when printing errors. (#102)
Motivation: BoringSSL's docs make it pretty clear that ERR_error_string is not safe to use in a concurrent context. Sadly, NIO is pretty damn concurrent, so we should probably stop using it. Modifications: - Rewrite the error printing logic to use ERR_error_string_n which takes a buffer to write into. Result: Error strings will not be corrupted in multithreaded contexts.
1 parent 4f9d64d commit 18b26df

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

Sources/NIOSSL/SSLErrors.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ public struct BoringSSLInternalError: Equatable, CustomStringConvertible {
1919
let errorCode: UInt32
2020

2121
var errorMessage: String? {
22-
if let cErrorMessage = CNIOBoringSSL_ERR_error_string(errorCode, nil) {
23-
return String.init(cString: cErrorMessage)
22+
// TODO(cory): This should become non-optional in the future, as it always succeeds.
23+
var scratchBuffer = [CChar](repeating: 0, count: 512)
24+
return scratchBuffer.withUnsafeMutableBufferPointer { pointer in
25+
CNIOBoringSSL_ERR_error_string_n(self.errorCode, pointer.baseAddress!, pointer.count)
26+
return String(cString: pointer.baseAddress!)
2427
}
25-
return nil
2628
}
2729

2830
public var description: String {

0 commit comments

Comments
 (0)