我正在开发一个应用程序,它需要一个RSA密钥来加密某些用户数据。我使用openssl,一切正常。但是,这个应用程序一直在警告RSA_new和RSA_generate_key_ex的内存泄漏(我认为这不应该是因为我释放了我创建的所有属性)。
下面是生成RSA密钥的代码:

BIGNUM e;
BN_init(&e);
BN_set_word(&e, 17);
RSA *rsa = RSA_new(); // Direct leak of 191 bytes in 1 object (RSA_new->RSA_new_method->...)
RAS_generate_key_ex(rsa, 1024, &e, NULL); // Indirect leak of 279 bytes in 1 object (RAS_generate_key_ex->rsa_builtin_keygen...)
EVP_PKEY pkey = EVP_PKEY_new();
EVP_PKEY_set1_RSA(pkey, rsa);
RSA_free(rsa);
BN_free(&e);
MINE_COPY_KEY(pkey); // I copy the pkey to other location at here //
EVP_PKEY_free(pkey);

我以为我分配的所有东西(e,pkey,rsa)都已经由“rsa-free,EVP-pkey-free,BN-free”发布了,但它仍然抱怨我的Linux x64机器内存泄漏

最佳答案

我尝试过你的程序(在修复了诸如RAS_generate_key_ex > > cc>和RSA_generate_key_ex > > cc>的拼写错误之后。
我现在有了这个消息来源:

#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>

int main() {
  BIGNUM e;
  BN_init(&e);
  BN_set_word(&e, 17);
  RSA *rsa = RSA_new();
  RSA_generate_key_ex(rsa, 1024, &e, NULL);
  EVP_PKEY* pkey = EVP_PKEY_new();
  EVP_PKEY_set1_RSA(pkey, rsa);
  RSA_free(rsa);
  BN_free(&e);
  //MINE_COPY_KEY(pkey); // I copy the pkey to other location at here //
  EVP_PKEY_free(pkey);
  return 0;
}

瓦尔格林说:
==18061==
==18061== LEAK SUMMARY:
==18061==    definitely lost: 0 bytes in 0 blocks
==18061==    indirectly lost: 0 bytes in 0 blocks
==18061==      possibly lost: 0 bytes in 0 blocks
==18061==    still reachable: 220 bytes in 6 blocks
==18061==         suppressed: 0 bytes in 0 blocks
==18061== Reachable blocks (those to which a pointer was found) are not shown.
==18061== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==18061==
==18061== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==18061== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

在程序末尾添加EVP_PKEY pkey = EVP_PKEY_new()使Valgring感到高兴:-)
==18212== HEAP SUMMARY:
==18212==     in use at exit: 0 bytes in 0 blocks
==18212==   total heap usage: 457 allocs, 457 frees, 31,748 bytes allocated
==18212==
==18212== All heap blocks were freed -- no leaks are possible
==18212==
==18212== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==18212== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

见:https://stackoverflow.com/a/21533000/6267288

关于c - RSA_new和RSA_generate_key_ex使内存泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44276494/

10-09 09:03