Skip to content

Commit

Permalink
ui/vnc: fix leak of SocketAddress **
Browse files Browse the repository at this point in the history
Extract the (correct) cleaning code as a new function vnc_free_addresses() then
use it to remove the memory leaks.

Reported-by: Clang Static Analyzer
Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
Reviewed-by: Daniel P. Berrange <[email protected]>
Signed-off-by: Michael Tokarev <[email protected]>
  • Loading branch information
philmd authored and Michael Tokarev committed Jul 31, 2017
1 parent f80ac75 commit 9f26f32
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions ui/vnc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3521,6 +3521,20 @@ static int vnc_display_get_address(const char *addrstr,
return ret;
}

static void vnc_free_addresses(SocketAddress ***retsaddr,
size_t *retnsaddr)
{
size_t i;

for (i = 0; i < *retnsaddr; i++) {
qapi_free_SocketAddress((*retsaddr)[i]);
}
g_free(*retsaddr);

*retsaddr = NULL;
*retnsaddr = 0;
}

static int vnc_display_get_addresses(QemuOpts *opts,
bool reverse,
SocketAddress ***retsaddr,
Expand All @@ -3538,7 +3552,6 @@ static int vnc_display_get_addresses(QemuOpts *opts,
bool has_ipv6 = qemu_opt_get(opts, "ipv6");
bool ipv4 = qemu_opt_get_bool(opts, "ipv4", false);
bool ipv6 = qemu_opt_get_bool(opts, "ipv6", false);
size_t i;
int displaynum = -1;
int ret = -1;

Expand Down Expand Up @@ -3614,16 +3627,8 @@ static int vnc_display_get_addresses(QemuOpts *opts,
ret = 0;
cleanup:
if (ret < 0) {
for (i = 0; i < *retnsaddr; i++) {
qapi_free_SocketAddress((*retsaddr)[i]);
}
g_free(*retsaddr);
for (i = 0; i < *retnwsaddr; i++) {
qapi_free_SocketAddress((*retwsaddr)[i]);
}
g_free(*retwsaddr);
*retsaddr = *retwsaddr = NULL;
*retnsaddr = *retnwsaddr = 0;
vnc_free_addresses(retsaddr, retnsaddr);
vnc_free_addresses(retwsaddr, retnwsaddr);
}
return ret;
}
Expand Down Expand Up @@ -3772,7 +3777,6 @@ void vnc_display_open(const char *id, Error **errp)
int acl = 0;
int lock_key_sync = 1;
int key_delay_ms;
size_t i;

if (!vd) {
error_setg(errp, "VNC display not active");
Expand Down Expand Up @@ -3993,12 +3997,8 @@ void vnc_display_open(const char *id, Error **errp)
}

cleanup:
for (i = 0; i < nsaddr; i++) {
qapi_free_SocketAddress(saddr[i]);
}
for (i = 0; i < nwsaddr; i++) {
qapi_free_SocketAddress(wsaddr[i]);
}
vnc_free_addresses(&saddr, &nsaddr);
vnc_free_addresses(&wsaddr, &nwsaddr);
return;

fail:
Expand Down

0 comments on commit 9f26f32

Please sign in to comment.