SM2中有内存泄漏?
内存泄漏检查使用了valgrind, valgrind --tool=memcheck --leak-check=full ./testGM
定位到pem_write() (pem.c), 应该是忘了free()了:->;
==41134== ==41134== HEAP SUMMARY: ==41134== in use at exit: 714 bytes in 2 blocks ==41134== total heap usage: 26 allocs, 24 frees, 102,351 bytes allocated ==41134== ==41134== 182 bytes in 1 blocks are definitely lost in loss record 1 of 2 ==41134== at 0x4C360A5: malloc (vg_replace_malloc.c:380) ==41134== by 0x44A4F8: pem_write (in /home/op01/workspace/coding/test/test_GM/build/testGM) ==41134== by 0x44006B: sm2_public_key_info_to_pem (in /home/op01/workspace/coding/test/test_GM/build/testGM) ==41134== by 0x421031: GM2Wrapper::wrtKeyPub(char const*) (in /home/op01/workspace/coding/test/test_GM/build/testGM) ==41134== by 0x420924: main (in /home/op01/workspace/coding/test/test_GM/build/testGM) ==41134== ==41134== 532 bytes in 1 blocks are definitely lost in loss record 2 of 2 ==41134== at 0x4C360A5: malloc (vg_replace_malloc.c:380) ==41134== by 0x44A4F8: pem_write (in /home/op01/workspace/coding/test/test_GM/build/testGM) ==41134== by 0x440878: sm2_private_key_info_encrypt_to_pem (in /home/op01/workspace/coding/test/test_GM/build/testGM) ==41134== by 0x4212DA: GM2Wrapper::wrtKeyPriv(char const*, char const*) (in /home/op01/workspace/coding/test/test_GM/build/testGM) ==41134== by 0x420954: main (in /home/op01/workspace/coding/test/test_GM/build/testGM) ==41134== ==41134== LEAK SUMMARY: ==41134== definitely lost: 714 bytes in 2 blocks ==41134== indirectly lost: 0 bytes in 0 blocks ==41134== possibly lost: 0 bytes in 0 blocks ==41134== still reachable: 0 bytes in 0 blocks ==41134== suppressed: 0 bytes in 0 blocks ==41134== ==41134== For lists of detected and suppressed errors, rerun with: -s ==41134== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
base64编码的时候,malloc后,没有free,这个导致的内存泄露
附议,感觉GmSSL3库在内存安全性这块,需要用valgrind工具看看
valgrind --leak-check=full --track-origins=yes --error-exitcode=1 bin/demo_sm2_encrypt ==4646== Memcheck, a memory error detector ==4646== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==4646== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==4646== Command: bin/demo_sm2_encrypt ==4646== ==4646== Source and destination overlap in memcpy(0x1ffefffb10, 0x1ffefffb10, 192) ==4646== at 0x4C2E81D: memcpy@@GLIBC_2.14 (vg_replace_strmem.c:1035) ==4646== by 0x41BA9C: sm2_jacobian_point_dbl (sm2_alg.c:896) ==4646== by 0x41C1EC: sm2_jacobian_point_mul (sm2_alg.c:1017) ==4646== by 0x41C328: sm2_jacobian_point_mul_generator (sm2_alg.c:1044) ==4646== by 0x40196C: sm2_key_generate (sm2_key.c:49) ==4646== by 0x4012A8: main (demo_sm2_encrypt.c:25) ==4646== ==4646== Source and destination overlap in memcpy(0x1ffefff840, 0x1ffefff840, 64) ==4646== at 0x4C2E81D: memcpy@@GLIBC_2.14 (vg_replace_strmem.c:1035) ==4646== by 0x419C25: sm2_fp_div2 (sm2_alg.c:377) ==4646== by 0x41BBB6: sm2_jacobian_point_dbl (sm2_alg.c:910) ==4646== by 0x41C1EC: sm2_jacobian_point_mul (sm2_alg.c:1017) ==4646== by 0x41C328: sm2_jacobian_point_mul_generator (sm2_alg.c:1044) ==4646== by 0x40196C: sm2_key_generate (sm2_key.c:49) ==4646== by 0x4012A8: main (demo_sm2_encrypt.c:25) ==4646== ==4646== Source and destination overlap in memcpy(0x1ffefff780, 0x1ffefff780, 192) ==4646== at 0x4C2E81D: memcpy@@GLIBC_2.14 (vg_replace_strmem.c:1035) ==4646== by 0x41BA9C: sm2_jacobian_point_dbl (sm2_alg.c:896) ==4646== by 0x41C1EC: sm2_jacobian_point_mul (sm2_alg.c:1017) ==4646== by 0x405166: sm2_do_encrypt (sm2_lib.c:523) ==4646== by 0x406180: sm2_encrypt (sm2_lib.c:801) ==4646== by 0x4012F0: main (demo_sm2_encrypt.c:28) ==4646== ciphertext: 3074022023820A143B3BF858CC60B7466D4315B82A6CF9F3752076001087C09A1DF73CEC022100D17E0F4461E2B04B91E37CDFCA89AE2C252EBF2C34E323A8EB17176A4E6FBEF10420AC3BC85C16AB7F446D396D0FC9153C1C5DD916F391D029E719409CD8B2108848040B3EDB02333B58DB7436D82E ==4646== Source and destination overlap in memcpy(0x1ffefff8f0, 0x1ffefff8f0, 192) ==4646== at 0x4C2E81D: memcpy@@GLIBC_2.14 (vg_replace_strmem.c:1035) ==4646== by 0x41BA9C: sm2_jacobian_point_dbl (sm2_alg.c:896) ==4646== by 0x41C1EC: sm2_jacobian_point_mul (sm2_alg.c:1017) ==4646== by 0x405758: sm2_do_decrypt (sm2_lib.c:660) ==4646== by 0x4064D1: sm2_decrypt (sm2_lib.c:851) ==4646== by 0x40134D: main (demo_sm2_encrypt.c:31)