内存泄露在OpenSSL中?(Memory leak in OpenSSL?)
我使用以下代码检查签名。 为简单起见,公钥,消息和签名在
main
中初始化。#include <openssl/sha.h> #include <openssl/rsa.h> #include <openssl/pem.h> #include <openssl/evp.h> int main(){ const unsigned char key[] = "-----BEGIN PUBLIC KEY-----\n" "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALulMfYFyX1kSm7oUxZyCWWhrBBcWrRA\n" "V7LSz1PzMPxoxG3KS8H7PRKFkIk42yM8/vhobmcCmj7UM5572wWch50CAwEAAQ==\n" "-----END PUBLIC KEY-----\n"; unsigned int key_len = sizeof(key); const unsigned char data[] = {0x6d,0x65,0x73,0x65,0x0a}; unsigned int data_len = sizeof(data); const unsigned char sig[] = {0xa9,0x29,0x81,0x07,0x8c,0xeb,0xf0,0x1b,0x2a,0x31,0xe5,0x60,0x94,0x8a,0x47,0x94,0x3a,0x8f,0x6b, 0x4e,0x85,0xb9,0xe7,0xe5,0x4a,0x6c,0x56,0x46,0xd1,0x80,0x15,0x57,0xce,0xcb,0x0a,0x3a,0x67,0x15,0xed, 0x68,0x03,0x58,0x99,0xa4,0x73,0x61,0xe3,0x30,0x85,0xff,0x89,0x7e,0x32,0xef,0x16,0xec,0x23,0x7f,0x14, 0xde,0xbf,0x53,0xe0,0x3a}; unsigned int sig_len = sizeof(sig); EVP_PKEY* evp_pubkey = EVP_PKEY_new(); RSA* rsa_pubkey = NULL; EVP_MD_CTX ctx; BIO* bufio = BIO_new_mem_buf((void*)key, key_len); if(bufio == NULL){ fprintf(stderr, "BIO not created.\n"); EVP_PKEY_free(evp_pubkey); BIO_free(bufio); return 3; } evp_pubkey = PEM_read_bio_PUBKEY(bufio, NULL, NULL, NULL); if(evp_pubkey == NULL){ fprintf(stderr, "evp_pubkey not created.\n"); EVP_PKEY_free(evp_pubkey); BIO_free(bufio); return 4; } EVP_MD_CTX_init(&ctx); if (!EVP_VerifyInit(&ctx, EVP_sha256())) { fprintf(stderr, "EVP_SignInit: failed.\n"); EVP_PKEY_free(evp_pubkey); BIO_free(bufio); return 5; } if (!EVP_VerifyUpdate(&ctx, data, data_len)) { fprintf(stderr, "EVP_SignUpdate: failed.\n"); EVP_PKEY_free(evp_pubkey); BIO_free(bufio); return 6; } if (!EVP_VerifyFinal(&ctx, sig, sig_len, evp_pubkey)) { fprintf(stderr, "EVP_VerifyFinal: failed.\n"); EVP_PKEY_free(evp_pubkey); BIO_free(bufio); return 7; } EVP_PKEY_free(evp_pubkey); BIO_free(bufio); return 0; }
在使用
gcc evp.c -lssl -lcrypto
编译并运行valgrind --tool=memcheck --leak-check=full ./a.out
之后,我得到一些内存泄漏:==7492== 56 bytes in 1 blocks are definitely lost in loss record 9 of 12 ==7492== at 0x4C2BBCF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==7492== by 0x4E9AD77: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==7492== by 0x4F5D459: EVP_PKEY_new (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==7492== by 0x400C4C: main (in a.out) ==7492== ==7492== 120 bytes in 1 blocks are definitely lost in loss record 10 of 12 ==7492== at 0x4C2BBCF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==7492== by 0x4E9AD77: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==7492== by 0x4F55678: EVP_DigestInit_ex (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==7492== by 0x400D6A: main (in a.out)
难道我做错了什么?
I use the following code to check a signature. For simplicity public key, message and signature are initialized in
main
.#include <openssl/sha.h> #include <openssl/rsa.h> #include <openssl/pem.h> #include <openssl/evp.h> int main(){ const unsigned char key[] = "-----BEGIN PUBLIC KEY-----\n" "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALulMfYFyX1kSm7oUxZyCWWhrBBcWrRA\n" "V7LSz1PzMPxoxG3KS8H7PRKFkIk42yM8/vhobmcCmj7UM5572wWch50CAwEAAQ==\n" "-----END PUBLIC KEY-----\n"; unsigned int key_len = sizeof(key); const unsigned char data[] = {0x6d,0x65,0x73,0x65,0x0a}; unsigned int data_len = sizeof(data); const unsigned char sig[] = {0xa9,0x29,0x81,0x07,0x8c,0xeb,0xf0,0x1b,0x2a,0x31,0xe5,0x60,0x94,0x8a,0x47,0x94,0x3a,0x8f,0x6b, 0x4e,0x85,0xb9,0xe7,0xe5,0x4a,0x6c,0x56,0x46,0xd1,0x80,0x15,0x57,0xce,0xcb,0x0a,0x3a,0x67,0x15,0xed, 0x68,0x03,0x58,0x99,0xa4,0x73,0x61,0xe3,0x30,0x85,0xff,0x89,0x7e,0x32,0xef,0x16,0xec,0x23,0x7f,0x14, 0xde,0xbf,0x53,0xe0,0x3a}; unsigned int sig_len = sizeof(sig); EVP_PKEY* evp_pubkey = EVP_PKEY_new(); RSA* rsa_pubkey = NULL; EVP_MD_CTX ctx; BIO* bufio = BIO_new_mem_buf((void*)key, key_len); if(bufio == NULL){ fprintf(stderr, "BIO not created.\n"); EVP_PKEY_free(evp_pubkey); BIO_free(bufio); return 3; } evp_pubkey = PEM_read_bio_PUBKEY(bufio, NULL, NULL, NULL); if(evp_pubkey == NULL){ fprintf(stderr, "evp_pubkey not created.\n"); EVP_PKEY_free(evp_pubkey); BIO_free(bufio); return 4; } EVP_MD_CTX_init(&ctx); if (!EVP_VerifyInit(&ctx, EVP_sha256())) { fprintf(stderr, "EVP_SignInit: failed.\n"); EVP_PKEY_free(evp_pubkey); BIO_free(bufio); return 5; } if (!EVP_VerifyUpdate(&ctx, data, data_len)) { fprintf(stderr, "EVP_SignUpdate: failed.\n"); EVP_PKEY_free(evp_pubkey); BIO_free(bufio); return 6; } if (!EVP_VerifyFinal(&ctx, sig, sig_len, evp_pubkey)) { fprintf(stderr, "EVP_VerifyFinal: failed.\n"); EVP_PKEY_free(evp_pubkey); BIO_free(bufio); return 7; } EVP_PKEY_free(evp_pubkey); BIO_free(bufio); return 0; }
After compiling with
gcc evp.c -lssl -lcrypto
and runningvalgrind --tool=memcheck --leak-check=full ./a.out
on it I get some memory leaks:==7492== 56 bytes in 1 blocks are definitely lost in loss record 9 of 12 ==7492== at 0x4C2BBCF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==7492== by 0x4E9AD77: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==7492== by 0x4F5D459: EVP_PKEY_new (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==7492== by 0x400C4C: main (in a.out) ==7492== ==7492== 120 bytes in 1 blocks are definitely lost in loss record 10 of 12 ==7492== at 0x4C2BBCF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==7492== by 0x4E9AD77: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==7492== by 0x4F55678: EVP_DigestInit_ex (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==7492== by 0x400D6A: main (in a.out)
Am I doing something wrong?
原文:https://stackoverflow.com/questions/34794272
最满意答案
编辑ProjectB的代码文件...
或者,转到构建设置,搜索“前缀”并编辑告诉项目应该使用的.pch文件所在的路径。
Edit the code file for ProjectB...
Alternatively, go to the build settings, search for 'prefix' and edit the path which tells the project where the .pch file that it should use is.
相关问答
更多-
从设备获取framework.jar并将其放入您的项目中。 看起来你想用隐藏的API编译你的项目。 adb pull /system/framework/framework.jar 以下是参考资料: http : //devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-2-hacking-around/ Get framework.jar from the device and put it in your project. It ...
-
无法导入标题文件(Cannot import header file)[2022-03-06]
确保文件位于项目文件夹中。 编译器不使用添加到项目中的所有标题,而是使用标题搜索路径中可以找到的所有标题。 默认情况下,该路径被设置为项目文件夹。 因此,如果您的标题在搜索路径之外,编译器将无法看到它。 了解C编译器不仅仅能够将文件添加到Xcode项目中,而是有时需要使用文件结构(例如,用于头文件),这一点很重要。 一些很难找到的错误可能是由这个引起的 - 例如,当有两个相同名称的头文件时。 Make sure the file is located in the project folder. The c ... -
一旦我使用cocoapods安装所需的库,这个工作。 您可以在这里学习如何安装cocoapods。 我仍然坚持认为如果没有cocoapods我无法弄清楚如何安装这个并且我发现卸载一个添加了cocoapods的库更加痛苦而不是它的价值。 This worked once I used cocoapods to install the desired library. You can learn how to install cocoapods here. I'm still put-out that I co ...
-
在Maven项目中导入错误(import errors in maven project)[2022-07-05]
您可以尝试不同的解决方案: - 尝试其他版本的Hadoop库 - 删除用户文件夹中的完整.m2存储库并重新下载库 - 检查您的项目是否在您的IDE中配置为Maven项目 祝你好运! I tried all the possible options like removing old .m2 directory, update project, checked settings.xml etc but none of them seemed to help. So I finally installed th ... -
我有同样的问题。 最后我在我的subspec添加了subspec 。 它更像是一个为我工作的工作(我不称之为解决方案)。 根据文件: public_header_files A list of file patterns that should be used as public headers. These are the headers that will be exposed to the user’s project and from which documentation will b ...
-
在子项目中使用main项目的.h文件有点奇怪。 这将导致两个项目不能再分开。 那不是为之设计的项目/子项目结构。 那么,为什么不将所有这些类从主项目移到子项目中,如果是这样,你仍然可以在主项目中使用它们? 无论如何,如果你坚持在主项目中保留这些类,你是否尝试将这些.h文件添加到子项目中? 您不需要将它们复制到您的子项目中,只需将它们添加为参考即可。 That's a little bit weird to use main project's .h file in a sub-project. It wil ...
-
在桥接头中导入头文件名时,需要引用头文件名。 喜欢这个: #import "UIImageView+WebCache.h"; 这与在objc中导入头文件的语法完全相同。 You need to quote the header file name when importing it in the bridging header. Like this: #import "UIImageView+WebCache.h"; This is exactly the same syntax as importin ...
-
编辑ProjectB的代码文件... 或者,转到构建设置,搜索“前缀”并编辑告诉项目应该使用的.pch文件所在的路径。 Edit the code file for ProjectB... Alternatively, go to the build settings, search for 'prefix' and edit the path which tells the project where the .pch file that it should use is.
-
如何在项目中导入模块?(How can I import module in project?)[2023-09-05]
好的,那是我的错,我应该用 export PYTHONPATH=/Users/roger/python/base_crawler_py 代替 PYTHONPATH=/Users/roger/python/base_crawler_py OK, that is my fault, I should use export PYTHONPATH=/Users/roger/python/base_crawler_py instead of PYTHONPATH=/Users/roger/python/base ... -
好吧,我觉得现在真的很愚蠢,但我应该尝试过,而不只是在Stack Overflow上问它并期待答案。 并不是说我很懒,但我已经习惯于在Stack Overflow上提问,我的本能就是在遇到问题时立即在Stack上问一个问题,而不是先尝试自己找到解决方案。 回答: 是的有效! 您可以通过桥接头在Swift中使用Objective-C ++类。 现在我没有做任何复杂的事情,只是在Objective-C ++类中实现了一个方法,并将该文件包含在桥接头中并在ViewController.Swift中调用该方法,并且 ...