Skip to content

Commit 85d2367

Browse files
authored
go back to allocating strings to avoid leaks (#272)
1 parent 903cfed commit 85d2367

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

lib/Ziti.swift

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -411,20 +411,55 @@ import CZitiPrivate
411411
let refresh_interval = 90
412412
#endif
413413

414+
// convert key and id info to char * types that ziti-sdk-c can use.
415+
// also considered .withCString - https://stackoverflow.com/questions/31378120/convert-swift-string-into-cchar-pointer
416+
let ctrlPtr = UnsafeMutablePointer<Int8>.allocate(capacity: id.ztAPI.count + 1)
417+
ctrlPtr.initialize(from: id.ztAPI, count: id.ztAPI.count + 1)
418+
419+
let certPEMPtr = UnsafeMutablePointer<Int8>.allocate(capacity: certPEM.count + 1)
420+
certPEMPtr.initialize(from: certPEM, count: certPEM.count + 1)
421+
422+
let privKeyPEMPtr = UnsafeMutablePointer<Int8>.allocate(capacity: privKeyPEM.count + 1)
423+
privKeyPEMPtr.initialize(from: privKeyPEM, count: privKeyPEM.count + 1)
424+
425+
var caPEMPtr:UnsafeMutablePointer<Int8>? = nil // todo empty string
426+
if (id.ca != nil) {
427+
caPEMPtr = UnsafeMutablePointer<Int8>.allocate(capacity: id.ca!.count + 1)
428+
caPEMPtr!.initialize(from: id.ca!, count: id.ca!.count + 1)
429+
}
430+
414431
// set up the ziti_config with our cert, etc.
415432
var ctrls:model_list = model_list()
416433
id.ztAPIs?.forEach { c in
417-
model_list_append(&ctrls, c.cstring)
434+
let ctrlPtr = UnsafeMutablePointer<Int8>.allocate(capacity: c.count + 1)
435+
ctrlPtr.initialize(from: c, count: c.count + 1)
436+
model_list_append(&ctrls, ctrlPtr)
418437
}
419438

420-
// ziti_context_init copies strings (strdup) for its own use, so it's ok to use references to swift strings here.
421439
var zitiCfg = ziti_config(
422-
controller_url: id.ztAPI.cstring,
440+
controller_url: ctrlPtr,
423441
controllers: ctrls,
424-
id: ziti_id_cfg(cert: certPEM.cstring, key: privKeyPEM.cstring, ca: id.ca?.cstring, oidc: nil),
442+
id: ziti_id_cfg(cert: certPEMPtr, key: privKeyPEMPtr, ca: caPEMPtr, oidc: nil),
425443
cfg_source: nil)
426444

427445
var zitiStatus = ziti_context_init(&self.ztx, &zitiCfg)
446+
447+
ctrlPtr.deallocate()
448+
certPEMPtr.deallocate()
449+
privKeyPEMPtr.deallocate()
450+
caPEMPtr?.deallocate()
451+
452+
withUnsafeMutablePointer(to: &ctrls) { ctrlListPtr in
453+
var i = model_list_iterator(ctrlListPtr)
454+
while i != nil {
455+
let ctrlPtr = model_list_it_element(i)
456+
if let ctrl = UnsafeMutablePointer<CChar>(OpaquePointer(ctrlPtr)) {
457+
ctrl.deallocate()
458+
}
459+
i = model_list_it_next(i)
460+
}
461+
}
462+
428463
guard zitiStatus == Ziti.ZITI_OK else {
429464
let errStr = String(cString: ziti_errorstr(zitiStatus))
430465
log.error("unable to initialize Ziti context, \(zitiStatus): \(errStr)", function:"start()")
@@ -1135,16 +1170,3 @@ func scan<
11351170
}
11361171
return result
11371172
}
1138-
1139-
extension String {
1140-
// use only when scope of c string matches scope of swift string.
1141-
var cstring: UnsafePointer<CChar> {
1142-
(self as NSString).cString(using: String.Encoding.utf8.rawValue)!
1143-
}
1144-
// use when c string needs to outlive swift string. caller must deallocate() the returned buffer when no longer needed.
1145-
var allocatedcString: UnsafeMutablePointer<CChar> {
1146-
let buf = UnsafeMutablePointer<CChar>.allocate(capacity: self.count + 1)
1147-
buf.initialize(from: self, count: self.count + 1)
1148-
return buf
1149-
}
1150-
}

0 commit comments

Comments
 (0)