[PATCH 08/11] xen_xl.c: Use g_autofree more

Ján Tomko jtomko at redhat.com
Fri Jan 14 16:22:00 UTC 2022


On a Friday in 2022, Michal Privoznik wrote:
>There are few places inside src/libxl/xen_xl.c that can benefit
>from g_autofree. Let them use automatic memory freeing.
>
>Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
>---
> src/libxl/xen_xl.c | 17 ++++++-----------
> 1 file changed, 6 insertions(+), 11 deletions(-)
>

[...]

>@@ -952,13 +951,13 @@ xenParseXLChannel(virConf *conf, virDomainDef *def)
> {
>     virConfValue *list = virConfGetValue(conf, "channel");
>     virDomainChrDef *channel = NULL;
>-    char *name = NULL;
>-    char *path = NULL;
>
>     if (list && list->type == VIR_CONF_LIST) {
>         list = list->list;
>         while (list) {
>             g_autofree char *type = NULL;
>+            g_autofree char *name = NULL;
>+            g_autofree char *path = NULL;
>             char *key;
>
>             if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
>@@ -1003,7 +1002,6 @@ xenParseXLChannel(virConf *conf, virDomainDef *def)

There's a mix of g_auto with manual freeing here:

                 } else if (STRPREFIX(key, "name=")) {
                     int len = nextkey ? (nextkey - data) : strlen(data);
                     VIR_FREE(name);
                     name = g_strndup(data, len);
                 } else if (STRPREFIX(key, "path=")) {
                     int len = nextkey ? (nextkey - data) : strlen(data);
                     VIR_FREE(path);
                     path = g_strndup(data, len);
                 }

I would prefer either altering the code to take the first key into
account instead of the last one by checking:

   else if (!name && STRPREFIX...

similarly how we do in node-based XML parsers, or leaving these two
variables unconverted and leave the refactor for later.

Jano

>                 channel->source->data.nix.path = g_steal_pointer(&path);
>             } else if (STRPREFIX(type, "pty")) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20220114/db971505/attachment-0001.sig>


More information about the libvir-list mailing list