policy: fix memleak in lookup_callback() and cancelling

When the operation is cancelled, we must not touch user_data. Note that
NM_POLICY_GET_PRIVATE() theoretically doesn't dereference the pointer
(does it?) but doing pointer arithmetic on a dangling pointer is a very
ugly thing to do.

And of course, the memleak.

Fixes: 5c716c8af8
Fixes: a2cdf63204
(cherry picked from commit 3215508293)
(cherry picked from commit f1469558c0)
This commit is contained in:
Thomas Haller 2017-04-23 19:14:19 +02:00
parent bd72919b47
commit ca342ed61d

View file

@ -490,26 +490,24 @@ lookup_callback (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
NMPolicy *self = (NMPolicy *) user_data; NMPolicy *self;
NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self); NMPolicyPrivate *priv;
const char *hostname; gs_free char *hostname = NULL;
GError *error = NULL; gs_free_error GError *error = NULL;
hostname = g_resolver_lookup_by_address_finish (G_RESOLVER (source), result, &error); hostname = g_resolver_lookup_by_address_finish (G_RESOLVER (source), result, &error);
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
/* Don't touch policy; it may have been freed already */
g_error_free (error);
return; return;
}
self = user_data;
priv = NM_POLICY_GET_PRIVATE (self);
g_clear_object (&priv->lookup_cancellable);
if (hostname) if (hostname)
_set_hostname (self, hostname, "from address lookup"); _set_hostname (self, hostname, "from address lookup");
else { else
_set_hostname (self, NULL, error->message); _set_hostname (self, NULL, error->message);
g_error_free (error);
}
g_clear_object (&priv->lookup_cancellable);
} }
static void static void