Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 断言—;指向函数指针—;失败_C_Signals_Assertion_Lib - Fatal编程技术网

C 断言—;指向函数指针—;失败

C 断言—;指向函数指针—;失败,c,signals,assertion,lib,C,Signals,Assertion,Lib,我正在玩Signal的libsignal库,试图猜测如何编译和运行一个小玩具程序。然而,我被困在一开始。我知道我必须用指向函数的指针来填充一个变量,这些函数稍后将在库中使用,即使我试图复制库在其测试中所做的工作,我也看不出测试和代码之间的区别,以及我的程序在运行时失败的原因。我使用的代码如下所示: #include <stdlib> #include <signal/signal_protocol.h> #include <signal/key_helper.h&g

我正在玩Signal的libsignal库,试图猜测如何编译和运行一个小玩具程序。然而,我被困在一开始。我知道我必须用指向函数的指针来填充一个变量,这些函数稍后将在库中使用,即使我试图复制库在其测试中所做的工作,我也看不出测试和代码之间的区别,以及我的程序在运行时失败的原因。我使用的代码如下所示:

#include <stdlib>
#include <signal/signal_protocol.h>
#include <signal/key_helper.h>
#include <openssl/rand.h>

int random(uint8_t *data, size_t len, void *user_data)
{
    if(RAND_bytes(data, len)) {
        return 0;
    }
    else {
        return SG_ERR_UNKNOWN;
    }
}

int main(int argc, char **argv) {
    signal_crypto_provider provider = {
            .random_func = random
            /*.hmac_sha256_init_func = HMAC_CTX_new,
            .hmac_sha256_update_func = HMAC_Update,
            .hmac_sha256_final_func = HMAC_Final,
            .hmac_sha256_cleanup_func = HMAC_CTX_free,
            .sha512_digest_init_func = SHA512_Init,
            .sha512_digest_update_func = SHA512_Update,
            .sha512_digest_final_func = SHA512_Final,
            .sha512_digest_cleanup_func = EVP_MD_CTX_free,
            .encrypt_func = EVP_aes_256_cbc,
            .decrypt_func = EVP_aes_256_cbc,
            .user_data = 0*/
    };

    signal_context *global_context;
    signal_context_create(&global_context, 0);
    signal_context_set_crypto_provider(global_context, &provider);
    //signal_context_set_locking_functions(global_context, lock_function,
    //unlock_function);


    ratchet_identity_key_pair *identity_key_pair;
    uint32_t registration_id;
    signal_protocol_key_helper_pre_key_list_node *pre_keys_head;
    session_signed_pre_key *signed_pre_key;


    signal_protocol_key_helper_generate_identity_key_pair(
        &identity_key_pair,
        global_context
    );

    exit(EXIT_SUCCESS);

}
失败的断言是第二个,给了我以下错误:

signal_crypto_random: Assertion `context->crypto_provider.random_func' failed.
我能想到的唯一解释是——我对C有点陌生——指针不知何故没有指向我之前指定的函数。如果这个猜测是正确的,为什么会发生这种情况

检查他们的测试代码,并将其与我的代码进行比较,我看不出是什么造成了导致我的程序失败的关键区别。调试时,变量似乎具有正确的内容

多谢各位



signal\u context\u set\u crypto\u provider()中有一个检查:

if(!crypto_provider
        || !crypto_provider->hmac_sha256_init_func
        || !crypto_provider->hmac_sha256_update_func
        || !crypto_provider->hmac_sha256_final_func
        || !crypto_provider->hmac_sha256_cleanup_func) {
    return SG_ERR_INVAL;
}
因此,答案是不能让这些回调保持未设置状态,这就是为什么不会将任何内容复制到上下文中,
assert()
最终会触发


我理解为什么测试代码不检查返回代码,但在生产代码中,您需要检查返回值以关注错误-这被认为是一种良好的做法。

那么
信号上下文设置加密提供程序()的返回代码是什么?-22,代表
SG\u ERR\u INVAL
。哦,老兄。如果
provider
变量中未指定任何
hmac\u sha
函数,则函数返回该错误。是的,确实如此。这就是为什么我问我的问题。请看下面我的答案。
signal\u context*global\u context;信号上下文创建(全局上下文,0)
看起来第一个不应该是指针,或者第二个不应该有
&
。。。但是也许
signal\u context\u create()?函数原型将帮助
int-signal\u-context\u创建(signal\u-context**context,void*user\u数据)我会接受你的建议,不仅是生产代码,还有这些游戏测试。我一直在想,如果我们继续下去,忘记错误检查,然后。。。看看如果你那样做会发生什么。谢谢你的帮助。是的,也许宏会派上用场。类似于
CHECK\u RET(\u call)
扩展到
do{}while(0)带有
int-ret在该循环体中,如果((ret=\u call)!=0)
printf()
相应地。只是为了在每个类似的地方使用它。谢谢你的代码片段。它肯定会派上用场:)
void setup_test_crypto_provider(signal_context *context)
{
    signal_crypto_provider provider = {
            .random_func = test_random_generator,
            .hmac_sha256_init_func = test_hmac_sha256_init,
            .hmac_sha256_update_func = test_hmac_sha256_update,
            .hmac_sha256_final_func = test_hmac_sha256_final,
            .hmac_sha256_cleanup_func = test_hmac_sha256_cleanup,
            .sha512_digest_init_func = test_sha512_digest_init,
            .sha512_digest_update_func = test_sha512_digest_update,
            .sha512_digest_final_func = test_sha512_digest_final,
            .sha512_digest_cleanup_func = test_sha512_digest_cleanup,
            .encrypt_func = test_encrypt,
            .decrypt_func = test_decrypt,
            .user_data = 0
    };

    signal_context_set_crypto_provider(context, &provider);
}
int test_random_generator(uint8_t *data, size_t len, void *user_data)
{
    if(RAND_bytes(data, len)) {
        return 0;
    }
    else {
        return SG_ERR_UNKNOWN;
    }
}
if(!crypto_provider
        || !crypto_provider->hmac_sha256_init_func
        || !crypto_provider->hmac_sha256_update_func
        || !crypto_provider->hmac_sha256_final_func
        || !crypto_provider->hmac_sha256_cleanup_func) {
    return SG_ERR_INVAL;
}