BN_CTX_free() vs BN_CTX_end() EXC_BAD_ACCESS 异常
BN_CTX_free() vs BN_CTX_end() EXC_BAD_ACCESS exception
从 OpenSSL docs 开始,BN_CTX_end()
必须在 BN_CTX_free()
之前调用,但是在下面的示例中,当我 运行文档指定的顺序(注意文档说 在大多数情况下 ,但我不确定如何检查我是否应该在 BN_CTX_free()
之前调用 BN_CTX_end()
)
BIGNUM* util::math::find_seed_num(std::vector<int> lst, int lst_int, int index) {
//python: reduce((lambda x, y: x * y), lst[0:index]) % lst_int
BN_CTX* ctx;
ctx = BN_CTX_new();
cout << "\nsize of lst " << lst.size() << "\n";
BIGNUM *sum = BN_new();
BIGNUM *tmp = BN_new();
sum = BN_CTX_get(ctx);
BN_set_word(sum, lst[0]);
cout << "\n index: " << index << "\n";
for (int a = 1; a < index; a = a + 1) {
BN_set_word(tmp, lst[a]);
cout << "temp = " << BN_bn2dec(tmp) << "\n";
BN_mul(sum, sum, tmp, ctx);
cout << "sum = " << BN_bn2dec(sum) << "\n";
}
BIGNUM *result = BN_new();
BIGNUM *modulo = BN_new();
BN_set_word(modulo, lst_int);
BN_nnmod(result, sum, modulo, ctx);
cout << "\nsum: " << BN_bn2dec(result) << "\n";
BN_free(sum);
BN_free(result);
BN_free(modulo);
BN_free(tmp);
BN_CTX_end(ctx); //Running this produces the exception
BN_CTX_free(ctx); //Running this w/out the above line leads to no exception thrown
return result;
}
我是 C++ 的新手,因此我担心如果 BN_CTX_end();
没有被调用,上下文就没有被正确地释放。
我不是 OpenSSL 专家,但文档说
A function must call BN_CTX_start() first. Then, BN_CTX_get() may be called repeatedly to obtain temporary BIGNUMs. All BN_CTX_get() calls must be made before calling any other functions that use the ctx as an argument.
Finally, BN_CTX_end() must be called
所以尝试在BN_CTX_new
之后的开头添加一个BN_CTX_start
(参见https://www.openssl.org/docs/man1.0.2/man3/BN_CTX_start.html)
其次,来自文档:
When BN_CTX_end() is called, the BIGNUM pointers obtained from BN_CTX_get() become invalid.
因此您可能想尝试避免释放总和,因为一旦您调用 BN_CTX_end()
它将(至少据我所知)被释放
最后(小问题):
BIGNUM *sum = BN_new();
....
sum = BN_CTX_get(ctx);
首先你分配一个BIGNUM
,然后你用另一个指向另一个内存位置的指针覆盖这个指针。因此,您丢失了指针,造成内存泄漏,请考虑使用 BN_new()
(在这种情况下您必须释放)或 BN_CTX_get
我尝试使用 MSVC 2019 进行编译并调用 BN_CTX_start()
已经为我解决了这个问题。
从 OpenSSL docs 开始,BN_CTX_end()
必须在 BN_CTX_free()
之前调用,但是在下面的示例中,当我 运行文档指定的顺序(注意文档说 在大多数情况下 ,但我不确定如何检查我是否应该在 BN_CTX_free()
之前调用 BN_CTX_end()
)
BIGNUM* util::math::find_seed_num(std::vector<int> lst, int lst_int, int index) {
//python: reduce((lambda x, y: x * y), lst[0:index]) % lst_int
BN_CTX* ctx;
ctx = BN_CTX_new();
cout << "\nsize of lst " << lst.size() << "\n";
BIGNUM *sum = BN_new();
BIGNUM *tmp = BN_new();
sum = BN_CTX_get(ctx);
BN_set_word(sum, lst[0]);
cout << "\n index: " << index << "\n";
for (int a = 1; a < index; a = a + 1) {
BN_set_word(tmp, lst[a]);
cout << "temp = " << BN_bn2dec(tmp) << "\n";
BN_mul(sum, sum, tmp, ctx);
cout << "sum = " << BN_bn2dec(sum) << "\n";
}
BIGNUM *result = BN_new();
BIGNUM *modulo = BN_new();
BN_set_word(modulo, lst_int);
BN_nnmod(result, sum, modulo, ctx);
cout << "\nsum: " << BN_bn2dec(result) << "\n";
BN_free(sum);
BN_free(result);
BN_free(modulo);
BN_free(tmp);
BN_CTX_end(ctx); //Running this produces the exception
BN_CTX_free(ctx); //Running this w/out the above line leads to no exception thrown
return result;
}
我是 C++ 的新手,因此我担心如果 BN_CTX_end();
没有被调用,上下文就没有被正确地释放。
我不是 OpenSSL 专家,但文档说
A function must call BN_CTX_start() first. Then, BN_CTX_get() may be called repeatedly to obtain temporary BIGNUMs. All BN_CTX_get() calls must be made before calling any other functions that use the ctx as an argument.
Finally, BN_CTX_end() must be called
所以尝试在BN_CTX_new
之后的开头添加一个BN_CTX_start
(参见https://www.openssl.org/docs/man1.0.2/man3/BN_CTX_start.html)
其次,来自文档:
When BN_CTX_end() is called, the BIGNUM pointers obtained from BN_CTX_get() become invalid.
因此您可能想尝试避免释放总和,因为一旦您调用 BN_CTX_end()
最后(小问题):
BIGNUM *sum = BN_new();
....
sum = BN_CTX_get(ctx);
首先你分配一个BIGNUM
,然后你用另一个指向另一个内存位置的指针覆盖这个指针。因此,您丢失了指针,造成内存泄漏,请考虑使用 BN_new()
(在这种情况下您必须释放)或 BN_CTX_get
我尝试使用 MSVC 2019 进行编译并调用 BN_CTX_start()
已经为我解决了这个问题。