Question

this code fails with Semgentation Fault: 11, and I can't understand why

framework 'Cocoa'
framework 'CoreFoundation'
framework 'Security'
* keychainObject = Pointer.new_with_type('^{OpaqueSecKeychainRef}')
SecKeychainOpen("/Users/charbon/Library/Keychains/Josja.keychain",keychainObject)
SecKeychainLock(keychainObject)

I'm sure it has to do with the keychainObject type, because this works (it locks the default keychain).

SecKeychainLock(nil) 

I'm using the '^{OpaqueSecKeychainRef}' as the type of pointer because that's what the debugger told me it expected when I used a wrong type of pointer.

I hope solving this would help grasping the macruby / cocoa magic.

For reference, the complete output is

cobalt:~ charbon$ macirb Desktop/test.rb 
irb(main):001:0> framework 'Cocoa'
=> true
irb(main):002:0> framework 'CoreFoundation'
=> true
irb(main):003:0> framework 'Security'
=> true
irb(main):004:0> * keychainObject = Pointer.new_with_type('^{OpaqueSecKeychainRef}')
=> [#<Pointer:0x4007ac200>]
irb(main):005:0> SecKeychainOpen("/Users/charbon/Library/Keychains/Josja.keychain",keychainObject)
=> 0
irb(main):006:0> SecKeychainLock(keychainObject)
Segmentation fault: 11
Was it helpful?

Solution

If you were writing C you would have written

SecKeychainRef keyChainRef;
SecKeychainOpen("/path/to/...", &keychainRef);
SecKeychainLock(keyChainRef);

i.e. while SecKeychainOpen requires a pointer to a SecKeychainRef (so that the output parameter can be filled in), other apis just require a SecKeychainRef, so you need to dereference the pointer:

framework 'Security'
keychainObject = Pointer.new_with_type('^{OpaqueSecKeychainRef}')
SecKeychainOpen("/Users/charbon/Library/Keychains/Josja.keychain",keychainObject)
SecKeychainLock(keychainObject.value)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top