From cf72b90087bc25a4b1a37917814c979db5bc1810 Mon Sep 17 00:00:00 2001 From: Dave Hart Date: Fri, 4 Dec 2020 09:19:23 -0500 Subject: [PATCH] Revert string casts to copies --- lib/Ziti-Bridging-Header.h | 7 +++++-- lib/Ziti.swift | 32 ++++++++++++++++++++++---------- lib/ZitiPostureChecks.swift | 9 +++++++-- lib/ziti.c | 20 ++++++++++++++++---- 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/lib/Ziti-Bridging-Header.h b/lib/Ziti-Bridging-Header.h index ce3dd47..b5ceb98 100644 --- a/lib/Ziti-Bridging-Header.h +++ b/lib/Ziti-Bridging-Header.h @@ -28,5 +28,8 @@ extern void ziti_set_log(log_writer log, uv_loop_t *loop); extern int ziti_debug_level; extern void ziti_logger_wrapper(int level, const char *file, unsigned int line, const char *func, const char *msg); -char **castStringArray(char *const arr[], int count); -char *castString(const char *str); +char **copyStringArray(char *const arr[], int count); +void freeStringArray(char **arr); + +char *copyString(const char *str); +void freeString(char *str); diff --git a/lib/Ziti.swift b/lib/Ziti.swift index 1d93b85..2679af9 100644 --- a/lib/Ziti.swift +++ b/lib/Ziti.swift @@ -594,8 +594,9 @@ import Foundation mySelf?.perform { withArrayOfCStrings(macArray) { arr in - let cp = castStringArray(arr, Int32(arr.count)) + let cp = copyStringArray(arr, Int32(arr.count)) macCtx.cb(macCtx.ztx, ctx.id, cp, Int32(macArray.count)) + freeStringArray(cp) } } } @@ -622,11 +623,15 @@ import Foundation mySelf?.perform { // C SDK didn't use `const` for strings, so need to copy 'em - let cType = type != nil ? castString(type!.cString(using: .utf8)) : nil - let cVersion = version != nil ? castString(version!.cString(using: .utf8)) : nil - let cBuild = build != nil ? castString(build!.cString(using: .utf8)) : nil + let cType = type != nil ? copyString(type!.cString(using: .utf8)) : nil + let cVersion = version != nil ? copyString(version!.cString(using: .utf8)) : nil + let cBuild = build != nil ? copyString(build!.cString(using: .utf8)) : nil osCtx.cb(osCtx.ztx, osCtx.id, cType, cVersion, cBuild) + + freeString(cType) + freeString(cVersion) + freeString(cBuild) } } @@ -637,12 +642,14 @@ import Foundation } guard let query = mySelf?.postureChecks?.processQuery else { log.warn("query not configured", function: "onProcessQuery()") - cb(ztx, castString(id), nil, false, nil, nil, 0) + let cId = copyString(id) + cb(ztx, cId, nil, false, nil, nil, 0) + freeString(cId) return } let strPath = path != nil ? String(cString: path!) : "" - query(ZitiProcessContext(ztx, castString(id), cb), strPath, Ziti.onProcessResponse) + query(ZitiProcessContext(ztx, id, cb), strPath, Ziti.onProcessResponse) } static private let onProcessResponse:ZitiPostureChecks.ProcessResponse = { ctx, path, isRunning, hash, signers in @@ -654,17 +661,21 @@ import Foundation mySelf?.perform { // C SDK didn't use `const` for strings, so need to copy 'em - let cPath = castString(path.cString(using: .utf8)) - let cHash = hash != nil ? castString(hash!.cString(using: .utf8)) : nil + let cPath = copyString(path.cString(using: .utf8)) + let cHash = hash != nil ? copyString(hash!.cString(using: .utf8)) : nil if let signers = signers { withArrayOfCStrings(signers) { arr in - let cp = castStringArray(arr, Int32(arr.count)) + let cp = copyStringArray(arr, Int32(arr.count)) pCtx.cb(pCtx.ztx, ctx.id, cPath, isRunning, cHash, cp, Int32(signers.count)) + freeStringArray(cp) } } else { pCtx.cb(pCtx.ztx, pCtx.id, cPath, isRunning, cHash, nil, 0) } + + freeString(cPath) + freeString(cHash) } } @@ -690,8 +701,9 @@ import Foundation mySelf?.perform { // C SDK didn't use `const` for strings, so need to copy 'em - let cDomain = domain != nil ? castString(domain!.cString(using: .utf8)) : nil + let cDomain = domain != nil ? copyString(domain!.cString(using: .utf8)) : nil dCtx.cb(dCtx.ztx, dCtx.id, cDomain) + freeString(cDomain) } } diff --git a/lib/ZitiPostureChecks.swift b/lib/ZitiPostureChecks.swift index 212645d..10c993a 100644 --- a/lib/ZitiPostureChecks.swift +++ b/lib/ZitiPostureChecks.swift @@ -132,8 +132,13 @@ class ZitiOsContext : ZitiPostureContext { class ZitiProcessContext : ZitiPostureContext { let cb:ziti_pr_process_cb - init(_ ztx:OpaquePointer, _ id:UnsafeMutablePointer?, _ cb: @escaping ziti_pr_process_cb) { + let cId:UnsafeMutablePointer? + init(_ ztx:OpaquePointer, _ id:UnsafePointer, _ cb: @escaping ziti_pr_process_cb) { self.cb = cb - super.init(ztx, id) + cId = copyString(id) + super.init(ztx, cId) + } + deinit { + freeString(cId) } } diff --git a/lib/ziti.c b/lib/ziti.c index 16aff71..9031eca 100644 --- a/lib/ziti.c +++ b/lib/ziti.c @@ -29,12 +29,24 @@ void ziti_logger_wrapper(int level, const char *file, unsigned int line, const c const char** ziti_all_configs = _ziti_all; -char **castStringArray(char *const arr[], int count) { +char **copyStringArray(char *const arr[], int count) { if (count == 0) return 0; - return (char **)arr; + + size_t sz = sizeof(char*); + char **arrCpy = calloc((count + 1), sz); + memcpy(arrCpy, arr, count * sz); + return arrCpy; } -char *castString(const char *str) { - return (char *)str; +void freeStringArray(char **arr) { + if (arr) free(arr); +} + +char *copyString(const char *str) { + return str ? strdup(str) : NULL; +} + +void freeString(char *str) { + if (str) free(str); }