Skip to content

Commit

Permalink
Merge pull request #53 from openziti/cast_removal
Browse files Browse the repository at this point in the history
Convert string casts to copies
  • Loading branch information
smilindave26 authored Dec 4, 2020
2 parents 31b3bca + cf72b90 commit bc1da37
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
7 changes: 5 additions & 2 deletions lib/Ziti-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
32 changes: 22 additions & 10 deletions lib/Ziti.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -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)
}
}

Expand All @@ -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
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down
9 changes: 7 additions & 2 deletions lib/ZitiPostureChecks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,13 @@ class ZitiOsContext : ZitiPostureContext {

class ZitiProcessContext : ZitiPostureContext {
let cb:ziti_pr_process_cb
init(_ ztx:OpaquePointer, _ id:UnsafeMutablePointer<Int8>?, _ cb: @escaping ziti_pr_process_cb) {
let cId:UnsafeMutablePointer<Int8>?
init(_ ztx:OpaquePointer, _ id:UnsafePointer<Int8>, _ cb: @escaping ziti_pr_process_cb) {
self.cb = cb
super.init(ztx, id)
cId = copyString(id)
super.init(ztx, cId)
}
deinit {
freeString(cId)
}
}
20 changes: 16 additions & 4 deletions lib/ziti.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit bc1da37

Please sign in to comment.