c - 尝试检索值时,将指针传递给函数会导致段错误

标签 c pointers openssl segmentation-fault memory-address

我在使用以下代码时遇到段错误。我很确定这是因为函数试图使用变量的地址而不是它的值。我对指针有点陌生。

int main(int argc, char *argv[])
{

    EVP_PKEY        priv_key_p;
    X509_REQ        req_p;
    X509            cert;
    PKCS7           pkcs7;

        /*Need to store value in req_p and priv_key_p*/
    makecsr(&req_p, &priv_key_p, passphrase);

        /*Need to use value of req_p and priv_key_p*/
    create_cert(&req_p, &cert, &priv_key_p, passphrase);
}


int create_cert(X509_REQ *req_p, X509 *cert, EVP_PKEY *priv_key_p, char *passphrase)
{
    int i;
    long serial = 1;
    EVP_PKEY *pkey;
    const EVP_MD *digest;
    X509_NAME *name;
    X509V3_CTX ctx;

    /* verify signature on the request */
    if (!(pkey = X509_REQ_get_pubkey (req_p))) <--- Segmentation fault here!
        int_error ("Error getting public key from request");
    ....
}

使用GDB,makecsr执行后,我可以毫无问题地打印priv_key_p和req_p的值。

但是,在 create_cert 函数内部,我只能通过写入 p *priv_key_p/*req_p 来打印该值

错误

Program received signal SIGSEGV, Segmentation fault.
0xb7ebb747 in X509_REQ_get_pubkey ()
   from /lib/i386-linux-gnu/libcrypto.so.1.0.0

最佳答案

你的代码:

if (!(pkey = X509_REQ_get_pubkey (req_p))) <--- Segementation fault here!
    int_error ("Error getting public key from request");
    ....

试试这个:

if (!(pkey == X509_REQ_get_pubkey (req_p))) <--- Segementation fault here!
    int_error ("Error getting public key from request");
    ....

关于c - 尝试检索值时,将指针传递给函数会导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19293735/

相关文章:

c - 如何对使用结构的指针数组进行排序?

javax.net.ssl.SSLException - trustAnchors 参数必须非空

c - 试图理解排列生成

c - 如何在 C 中使用 char* 循环遍历字符?

C中多个数组的笛卡尔积

c - 在 Ubuntu LInux 中使用 C API 锁定和解锁文件

c - 为什么我们不需要取消引用命令行参数(简单)

c++ - 如何修改数组指针并将其作为参数传递给需要数组作为参数的函数?

c++ - 使用 OpenSSL 1.1.1d 静态编译 Qt 5.13.1 生成 QSslSocket::connectToHostEncrypted: TLS 初始化失败

encryption - 如何将二进制私钥文件转换为openssl可接受的pem格式