lib: Prefer g_autoptr(dnsmasqCaps) instead of explicit unref

The dnsmasqCaps type has its own cleanup function defined and
ready to use via g_autoptr(). Use automatic cleanup instead of
an explicit one.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
This commit is contained in:
Michal Privoznik 2022-01-11 16:51:32 +01:00
parent a7ffa2a647
commit 6944c78d38
2 changed files with 13 additions and 16 deletions

View File

@ -702,44 +702,42 @@ dnsmasqCapsRefreshInternal(dnsmasqCaps *caps, bool force)
static dnsmasqCaps * static dnsmasqCaps *
dnsmasqCapsNewEmpty(void) dnsmasqCapsNewEmpty(void)
{ {
dnsmasqCaps *caps; g_autoptr(dnsmasqCaps) caps = NULL;
if (dnsmasqCapsInitialize() < 0) if (dnsmasqCapsInitialize() < 0)
return NULL; return NULL;
if (!(caps = virObjectNew(dnsmasqCapsClass))) if (!(caps = virObjectNew(dnsmasqCapsClass)))
return NULL; return NULL;
caps->binaryPath = g_strdup(DNSMASQ); caps->binaryPath = g_strdup(DNSMASQ);
return caps; return g_steal_pointer(&caps);
} }
dnsmasqCaps * dnsmasqCaps *
dnsmasqCapsNewFromBuffer(const char *buf) dnsmasqCapsNewFromBuffer(const char *buf)
{ {
dnsmasqCaps *caps = dnsmasqCapsNewEmpty(); g_autoptr(dnsmasqCaps) caps = dnsmasqCapsNewEmpty();
if (!caps) if (!caps)
return NULL; return NULL;
if (dnsmasqCapsSetFromBuffer(caps, buf) < 0) { if (dnsmasqCapsSetFromBuffer(caps, buf) < 0)
virObjectUnref(caps);
return NULL; return NULL;
}
return caps; return g_steal_pointer(&caps);
} }
dnsmasqCaps * dnsmasqCaps *
dnsmasqCapsNewFromBinary(void) dnsmasqCapsNewFromBinary(void)
{ {
dnsmasqCaps *caps = dnsmasqCapsNewEmpty(); g_autoptr(dnsmasqCaps) caps = dnsmasqCapsNewEmpty();
if (!caps) if (!caps)
return NULL; return NULL;
if (dnsmasqCapsRefreshInternal(caps, true) < 0) { if (dnsmasqCapsRefreshInternal(caps, true) < 0)
virObjectUnref(caps);
return NULL; return NULL;
}
return caps; return g_steal_pointer(&caps);
} }
const char * const char *

View File

@ -112,8 +112,9 @@ static int
mymain(void) mymain(void)
{ {
int ret = 0; int ret = 0;
dnsmasqCaps *full g_autoptr(dnsmasqCaps) full = NULL;
= dnsmasqCapsNewFromBuffer("Dnsmasq version 2.67\n--bind-dynamic\n--ra-param");
full = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.67\n--bind-dynamic\n--ra-param");
#define DO_TEST(xname, xcaps) \ #define DO_TEST(xname, xcaps) \
do { \ do { \
@ -154,8 +155,6 @@ mymain(void)
DO_TEST("leasetime-hours", full); DO_TEST("leasetime-hours", full);
DO_TEST("leasetime-infinite", full); DO_TEST("leasetime-infinite", full);
virObjectUnref(full);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
} }