SecStaticCodeCreateWithPath returns -4960 when the path contains empty char

Hello

on MacOS, I use following codes to get the sign of an application

char app_path[PATH_MAX+1] = {0};
uint32_t size = sizeof(app_path);

int ret = _NSGetExecutablePath(app_path, &size);
if (ret)
        return -1;

NSString *app_path_str = [[NSString alloc] initWithUTF8String: app_path];
NSURL* url = [NSURL URLWithString:app_path_str];
CFURLRef path = (__bridge CFURLRef)url;

SecStaticCodeRef static_code;
OSStatus status = SecStaticCodeCreateWithPath(path, kSecCSDefaultFlags, &static_code);

and find when the path includes a space char( for example: "/Users/username/Downloads/test A/test.dmg") SecStaticCodeCreateWithPath will always return -4960(errSecCoreFoundationUnknown?)

and it works well when the path doesn't contain space, could anyone give some help?

thanks very much

Answered by JWWalker in 775471022

I think you should be using fileURLWithPath:, not URLWithString:.

Accepted Answer

I think you should be using fileURLWithPath:, not URLWithString:.

it works

thanks very much~

I’m glad that you’ve solved your problem but there’s a much better way. Instead of calling _NSGetExecutablePath, get your code object directly by calling SecCodeCopySelf. The resulting SecCodeRef is a ‘subclass’ of the SecStaticCodeRef. In C you can just pass a code object directly to a routine that expects a static code object and the system will get the static code object from it [1].

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Swift’s stricter type checking meanings that you need to call SecCodeCopyStaticCode.

SecStaticCodeCreateWithPath returns -4960 when the path contains empty char
 
 
Q