[英]Keychain access code leaking
Here is the leak: 这是泄漏:
And here is my code: 这是我的代码:
+(NSString *)getSecureValueForKey:(NSString *)key {
// Retrieve a value from the keychain
NSDictionary *result;
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecReturnAttributes, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, kCFBooleanTrue, nil] autorelease];
NSDictionary *query = [[NSDictionary alloc] initWithObjects: objects forKeys: keys];
// Check if the value was found
OSStatus status = SecItemCopyMatching((CFDictionaryRef) query, (CFTypeRef *) &result);
[query release];
if (status != noErr) {
// Value not found
return nil;
} else {
// Value was found so return it
NSString *value = (NSString *) [result objectForKey: (NSString *) kSecAttrGeneric];
return value;
[result release];
}
}
+(BOOL)storeSecureValue:(NSString *)value forKey:(NSString *)key {
// Get the existing value for the key
NSString *existingValue = [self getSecureValueForKey:key];
// Check if a value already exists for this key
OSStatus status;
if (existingValue) {
// Value already exists, so update it
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, nil] autorelease];
NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
status = SecItemUpdate((CFDictionaryRef) query, (CFDictionaryRef) [NSDictionary dictionaryWithObject:value forKey: (NSString *) kSecAttrGeneric]);
} else {
// Value does not exist, so add it
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecAttrGeneric, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, value, nil] autorelease];
NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
status = SecItemAdd((CFDictionaryRef) query, NULL);
}
// Check if the value was stored
if (status != noErr) {
// Value was not stored
return false;
} else {
// Value was stored
return true;
}
}
Can you help me fix it? 你能帮我解决吗? The leak is everytime I access or store data in the keychain.
每当我访问或将数据存储在钥匙串中时,就会发生泄漏。 I haven't programmed without ARC for a lot of time, and I just can't track this leak down!
我已经有很多时间没有使用ARC编程了,而且我无法追踪这种泄漏!
Thank you! 谢谢!
Release keys
and objects
after creating query
, release result
before returning value
, actually release new objects created during your store secure value method. 创建
query
后释放keys
和objects
,返回value
之前释放result
,实际上释放在存储安全值方法期间创建的新对象。
At this point, your options are, learn how memory management actually works, or just turn on ARC and let it handle most of that for you. 此时,您可以选择了解内存管理的实际工作方式,或者仅打开ARC并让它为您处理大部分事务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.