调用 SSL_connect 时发生泄漏

发布于 2024-10-19 02:37:06 字数 1971 浏览 5 评论 0原文

我正在使用 OpenSSL,根据 Instruments 的说法,我在 SSL_connect 处开始出现内存泄漏。

SSL_CTX *ctx = SSL_CTX_new(SSLv23_method());
if (!ctx) {
    NSLog(@"Could not initialize ctx");
    [self release];
    return nil;
}
if(!SSL_CTX_use_certificate_chain_file(ctx, [PEMFile UTF8String])) {
    NSLog(@"Can't read certificate file");
    [self release];
    return nil;
}
if(!(SSL_CTX_use_PrivateKey_file(ctx, [PEMFile UTF8String], SSL_FILETYPE_PEM))) {
    NSLog(@"Can't read key file");
    [self release];
    return nil;
}

_sock = [self _tcpConnectWithHost:host port:port];
if (_sock < 0) {
    [self release];
    return nil;
}
_sslPointer = SSL_new(ctx);
BIO *bio = BIO_new_socket(sock, BIO_NOCLOSE);
SSL_set_bio(_sslPointer, bio, bio);
if(SSL_connect(_sslPointer) <= 0) {
    NSLog(@"SSL connect error");
    [self release];
    return nil;
}

SSL_CTX_free(ctx);

然后我在调用 dealloc 时释放它。这就是泄漏出现的时候。

SSL_free(_sslPointer);
close(_sock);

泄漏开始的调用堆栈是:

0 libSystem.B.dylib malloc
1 libcrypto.0.9.8.dylib CRYPTO_malloc
2 libcrypto.0.9.8.dylib asn1_item_ex_combine_new
3 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
4 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
5 libcrypto.0.9.8.dylib asn1_template_ex_d2i
6 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
7 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
8 libcrypto.0.9.8.dylib asn1_template_ex_d2i
9 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
10 libcrypto.0.9.8.dylib x509_name_ex_d2i
11 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
12 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
13 libcrypto.0.9.8.dylib asn1_template_ex_d2i
14 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
15 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
16 libcrypto.0.9.8.dylib asn1_template_ex_d2i
17 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
18 libcrypto.0.9.8.dylib ASN1_item_d2i
19 libssl.0.9.8.dylib ssl3_get_server_certificate
20 libssl.0.9.8.dylib ssl3_connect
21 libssl.0.9.8.dylib ssl23_connect

I am using OpenSSL and according to Instruments, I have a memory leak started at SSL_connect.

SSL_CTX *ctx = SSL_CTX_new(SSLv23_method());
if (!ctx) {
    NSLog(@"Could not initialize ctx");
    [self release];
    return nil;
}
if(!SSL_CTX_use_certificate_chain_file(ctx, [PEMFile UTF8String])) {
    NSLog(@"Can't read certificate file");
    [self release];
    return nil;
}
if(!(SSL_CTX_use_PrivateKey_file(ctx, [PEMFile UTF8String], SSL_FILETYPE_PEM))) {
    NSLog(@"Can't read key file");
    [self release];
    return nil;
}

_sock = [self _tcpConnectWithHost:host port:port];
if (_sock < 0) {
    [self release];
    return nil;
}
_sslPointer = SSL_new(ctx);
BIO *bio = BIO_new_socket(sock, BIO_NOCLOSE);
SSL_set_bio(_sslPointer, bio, bio);
if(SSL_connect(_sslPointer) <= 0) {
    NSLog(@"SSL connect error");
    [self release];
    return nil;
}

SSL_CTX_free(ctx);

I then release it when dealloc is called. This is when the leak appears.

SSL_free(_sslPointer);
close(_sock);

Call stack for where the leak starts is:

0 libSystem.B.dylib malloc
1 libcrypto.0.9.8.dylib CRYPTO_malloc
2 libcrypto.0.9.8.dylib asn1_item_ex_combine_new
3 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
4 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
5 libcrypto.0.9.8.dylib asn1_template_ex_d2i
6 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
7 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
8 libcrypto.0.9.8.dylib asn1_template_ex_d2i
9 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
10 libcrypto.0.9.8.dylib x509_name_ex_d2i
11 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
12 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
13 libcrypto.0.9.8.dylib asn1_template_ex_d2i
14 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
15 libcrypto.0.9.8.dylib asn1_template_noexp_d2i
16 libcrypto.0.9.8.dylib asn1_template_ex_d2i
17 libcrypto.0.9.8.dylib ASN1_item_ex_d2i
18 libcrypto.0.9.8.dylib ASN1_item_d2i
19 libssl.0.9.8.dylib ssl3_get_server_certificate
20 libssl.0.9.8.dylib ssl3_connect
21 libssl.0.9.8.dylib ssl23_connect

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-10-26 02:37:06

我看到的最明显的事情是,只有在一切正常的情况下,您才可以释放 SSL_CTX *ctx 。但是,在创建 ctx 之前,您有很多可能的退出,但您从未 free() 它。

The most obvious thing I see is that you're only ever freeing the SSL_CTX *ctx if everything works out OK. But you have a whole bunch of possible exits before that where ctx has been created, but you never free() it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文