qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: zhanghailiang <zhang.zhanghailiang@huawei.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: qemu-trivial@nongnu.org, mjt@tls.msk.ru, qemu-devel@nongnu.org,
	peter.huangpeng@huawei.com
Subject: Re: [Qemu-devel] [PATCH v2] libcacard: fix resource leak
Date: Mon, 17 Nov 2014 13:18:13 +0800	[thread overview]
Message-ID: <54698515.9040203@huawei.com> (raw)
In-Reply-To: <87oasa2kqh.fsf@blackfin.pond.sub.org>

On 2014/11/14 17:29, Markus Armbruster wrote:
> zhanghailiang <zhang.zhanghailiang@huawei.com> writes:
>
>> In function connect_to_qemu(), getaddrinfo() will allocate memory
>> that is stored into server, it should be freed by using freeaddrinfo()
>> before connect_to_qemu() return.
>>
>> Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
>> ---
>> v2:
>> - fix typo in title ;)
>> ---
>>   libcacard/vscclient.c | 7 ++++++-
>>   1 file changed, 6 insertions(+), 1 deletion(-)
>>
>> diff --git a/libcacard/vscclient.c b/libcacard/vscclient.c
>> index 80111df..fa6041d 100644
>> --- a/libcacard/vscclient.c
>> +++ b/libcacard/vscclient.c
>> @@ -597,7 +597,7 @@ connect_to_qemu(
>>       const char *port
>>   ) {
>>       struct addrinfo hints;
>> -    struct addrinfo *server;
>> +    struct addrinfo *server = NULL;
>>       int ret, sock;
>>
>>       sock = socket(AF_INET, SOCK_STREAM, 0);
>> @@ -629,9 +629,14 @@ connect_to_qemu(
>>       if (verbose) {
>>           printf("Connected (sizeof Header=%zd)!\n", sizeof(VSCMsgHeader));
>>       }
>> +
>> +    freeaddrinfo(server);
>>       return sock;
>>
>>   cleanup_socket:
>> +    if (server) {
>> +        freeaddrinfo(server);
>> +    }
>>       closesocket(sock);
>>       return -1;
>>   }
>
> Reviewed-by: Markus Armbruster <armbru@redhat.com>
>
> Aside: this code uses the first result from getaddrinfo(), and fails if
> it can't connect.  This is a common misuse of getaddrinfo().
>
> Consider a remote host with both an IPv4 and an IPv6 address, but only
> one of them actually connects.  If getaddrinfo() puts the connectable
> one first, we succeed.  Else, we fail.
>
> We should try the addresses in order until connect() succeeds, like
> qemu-sockets.c does.

Good catch, i will fix it soon. And this patch mainly fix the coverity warning.
Maybe i should fix the problem after this patch been merged. ;) Thanks.

      reply	other threads:[~2014-11-17  5:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-14  2:18 [Qemu-devel] [PATCH v2] libcacard: fix resource leak zhanghailiang
2014-11-14  9:29 ` Markus Armbruster
2014-11-17  5:18   ` zhanghailiang [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=54698515.9040203@huawei.com \
    --to=zhang.zhanghailiang@huawei.com \
    --cc=armbru@redhat.com \
    --cc=mjt@tls.msk.ru \
    --cc=peter.huangpeng@huawei.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).