简体   繁体   English

使用头文件中声明的字符串时为EXC_BAD_ACCESS

[英]EXC_BAD_ACCESS when using string declared in header file

I've declared a string in my header file like so: 我在头文件中声明了一个字符串,如下所示:

@property (nonatomic, retain) NSString *resultOfHash;

I call my getHash method like so: 我这样调用我的getHash方法:

 NSString *hash = [self getHash];

My getHash method is: 我的getHash方法是:

-(NSString *) getHash
{
//Get username form Keychain
KeychainItemWrapper *keyChain = [[KeychainItemWrapper alloc]    initWithIdentifier:KeyChainName accessGroup:nil];
username = [keyChain objectForKey:(__bridge id)kSecAttrAccount];

//get token from NSUserDefauls
NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:@"Token"];

NSString *toHash = [[username stringByAppendingString:HashExtra] stringByAppendingString:token];

const char *s = [toHash cStringUsingEncoding:NSASCIIStringEncoding];

NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];

uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};

CC_SHA512(keyData.bytes, keyData.length, digest);

NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];

//convert to string
resultOfHash = [out description];
//App crashed out above

// get rid of unwanted characters
resultOfHash = [resultOfHash stringByReplacingOccurrencesOfString:@" " withString:@""];
resultOfHash = [resultOfHash stringByReplacingOccurrencesOfString:@"<" withString:@""];
resultOfHash = [resultOfHash stringByReplacingOccurrencesOfString:@">" withString:@""];

//log to make sure it works
NSLog(@"hash is: %@", resultOfHash);

return resultOfHash;
}

My code crashes out at the line: ResultOfHash = [out description]; 我的代码在以下行崩溃:ResultOfHash = [out description]; but I'm not sure why. 但我不确定为什么。

When I use a local variable the conversion works fine but then I cannot return the local variable from the getHash method. 当我使用局部变量时,转换工作正常,但随后我无法从getHash方法返回局部变量。 Example: 例:

Replace ResultOfHash = [out description]; 替换ResultOfHash = [out description];

with

NSString *local = [out description];
return local;

and the conversion works fine and when I debug line by line, the debugger will go to my closing bracket on my method and then produce the EXC_BAD_ACCESS error. 并且转换工作正常,当我逐行调试时,调试器将转到我方法的右括号,然后产生EXC_BAD_ACCESS错误。

I've tried running NSZombie but that didn't find anything at all. 我试过运行NSZombie,但是根本找不到任何东西。

Any help in trying to sort this out would be greatly appreciated. 试图解决此问题的任何帮助将不胜感激。

Have a look at the answer in this question . 看一下这个问题的答案 Try converting to NSString using 尝试使用转换为NSString

[NSString *local = [[[NSString alloc] initWithData:out encoding:NSASCIIStringEncoding]; 

I haven't tested this code with this encoding, but it's similar to something I already use. 我尚未使用此编码测试此代码,但它与我已经使用的东西相似。

Update - 更新-

I corrected an error in the code above. 我更正了上面代码中的错误。 I somehow left the method signiture out in a distracted copy and paste. 我莫名其妙地将方法签名留在了分散的副本和粘贴中。

It's crashing because out is unretained. 崩溃是因为out保留。 You should add retain: 您应该添加保留:

resultOfHash = [[out description] retain];

or use retained property: 或使用保留的财产:

self.resultOfHash = [out description];

Check this, it should be work. 检查一下,应该可以了。

您可能需要使用NSMutableString

I think the problem is here: 我认为问题出在这里:

uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
CC_SHA512(keyData.bytes, keyData.length, digest);
NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];

You are using CC_SHA512 , but only allocate array of size CC_SHA1_DIGEST_LENGTH , which is smaller and will lead to the buffer overrunning. 您正在使用CC_SHA512 ,但仅分配大小为CC_SHA1_DIGEST_LENGTH数组,该数组较小,将导致缓冲区溢出。

To correct this, you should use CC_SHA512_DIGEST_LENGTH instead. 若要更正此问题,应改用CC_SHA512_DIGEST_LENGTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM