Memcpy 仅在 iPhone 5s 上崩溃 [英] Memcpy crash only on iPhone 5s

查看:25
本文介绍了Memcpy 仅在 iPhone 5s 上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我在使用 iPhone 5s 时遇到了一个奇怪的问题.我正在使用第三方库在我的应用程序中进行 HMAC-SHA1 加密.该库使用 memcpy,它不知道它是什么,因为我对 C 中的内存级编程没有过多的兴趣.除了 64 位 5s 之外,加密在所有 iphone 中都可以正常工作.下面是它崩溃的代码(5th line).

I am facing a strange issue with iPhone 5s. I am doing a HMAC-SHA1 encrypting in my app using a third party library. The library uses memcpy, which am not aware what it is, as I am not into too much of memory level programming in C. The encryption works perfectly fine in all iphones except the 64 bit 5s. Below is the code where it crashes (5th line).

void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len)
{
unsigned int i, j;

    j = (context->count[0] >> 3) & 63;
    if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
    context->count[1] += (len >> 29);
    if ((j + len) > 63) {
        memcpy(&context->buffer[j], data, (i = 64-j));
        SHA1Transform(context->state, context->buffer);
        for ( ; i + 63 < len; i += 64) {
            SHA1Transform(context->state, &data[i]);
        }
        j = 0;
    }
    else i = 0;
    memcpy(&context->buffer[j], &data[i], len - i);
}

以下是崩溃的详细信息

异常类型:EXC_BAD_ACCESS (SIGSEGV)异常子类型:KERN_INVALID_ADDRESS 在 0x001a9c799cbfdcf9由线程触发:0

Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Subtype: KERN_INVALID_ADDRESS at 0x001a9c799cbfdcf9 Triggered by Thread: 0

线程 0 崩溃:0 libsystem_platform.dylib 0x0000000191c34dc8 _platform_memmove + 401 Anyvan 0x00000001001fc6a8 SHA1Update (sha1.c:128)2 Anyvan 0x00000001000c7efc hmac_sha1 (hmac.c:73)3 Anyvan 0x00000001000fe7cc -[LoginViewController callAuth:] (LoginViewController.m:360)

Thread 0 Crashed: 0 libsystem_platform.dylib 0x0000000191c34dc8 _platform_memmove + 40 1 Anyvan 0x00000001001fc6a8 SHA1Update (sha1.c:128) 2 Anyvan 0x00000001000c7efc hmac_sha1 (hmac.c:73) 3 Anyvan 0x00000001000fe7cc -[LoginViewController callAuth:] (LoginViewController.m:360)

任何帮助将不胜感激.

推荐答案

这会崩溃,因为您使用的是 int 而不是 memcpy.尝试用 NSInteger 替换 int ,它应该可以工作.您可以在此处找到更多信息.

This is crashing because you are using int not because of memcpy. Try replacing the int with NSInteger and it should work. You can find more information here.

iPhone5S 使用 64 位架构,而其他人使用 32 位.两者之间的核心区别之一是系统如何处理文档中解释的整数.您的代码正在使用 unsigned int 这在 32 位和 64 位上并不意味着相同的事情,这会导致崩溃.您应该将名为 ijint 更改为 NSIntegers,我相信您的代码会起作用.或者想一想,您可以尝试简单地将 int 替换为 long.

The iPhone5S uses a 64bit architecture while the others use 32bit. One of the core differences between the two is how the system handles integers as explained in the doc. Your code is using unsigned int which does not mean the same thing on 32bit and on 64bit and that results in a crash. You should change your int called i and j to NSIntegers and I believe your code will work. Or come to think of it you can try to simply replace the word int by long.

这篇关于Memcpy 仅在 iPhone 5s 上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆