[PATCH] src: Avoid needless checks before calling g_strdup()

Michal Privoznik posted 1 patch 7 months, 1 week ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/1a81790d809f6f0f0b808b3ec6a64eb1940829e7.1695029800.git.mprivozn@redhat.com
src/conf/domain_event.c      | 3 +--
src/libxl/libxl_conf.c       | 6 ++----
src/lxc/lxc_native.c         | 3 +--
src/qemu/qemu_monitor_json.c | 3 +--
src/util/virconf.c           | 3 +--
5 files changed, 6 insertions(+), 12 deletions(-)
[PATCH] src: Avoid needless checks before calling g_strdup()
Posted by Michal Privoznik 7 months, 1 week ago
There are few places where the following pattern occurs:

  if (var)
      other = g_strdup(var);

where @other wasn't initialized before g_strdup(). Checking for
var != NULL is useless in this case, as that's exactly what
g_strdup() does (in which case it returns NULL).

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/conf/domain_event.c      | 3 +--
 src/libxl/libxl_conf.c       | 6 ++----
 src/lxc/lxc_native.c         | 3 +--
 src/qemu/qemu_monitor_json.c | 3 +--
 src/util/virconf.c           | 3 +--
 5 files changed, 6 insertions(+), 12 deletions(-)

diff --git a/src/conf/domain_event.c b/src/conf/domain_event.c
index 75603a933a..09f3368064 100644
--- a/src/conf/domain_event.c
+++ b/src/conf/domain_event.c
@@ -1561,8 +1561,7 @@ virDomainEventMetadataChangeNew(int id,
         return NULL;
 
     ev->type = type;
-    if (nsuri)
-        ev->nsuri = g_strdup(nsuri);
+    ev->nsuri = g_strdup(nsuri);
 
     return (virObjectEvent *)ev;
 }
diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c
index 3ffb46fddd..4582126d19 100644
--- a/src/libxl/libxl_conf.c
+++ b/src/libxl/libxl_conf.c
@@ -583,8 +583,7 @@ libxlMakeDomBuildInfo(virDomainDef *def,
 #endif
 
         /* copy SLIC table path to acpi_firmware */
-        if (def->os.slic_table)
-            b_info->u.hvm.acpi_firmware = g_strdup(def->os.slic_table);
+        b_info->u.hvm.acpi_firmware = g_strdup(def->os.slic_table);
 
         if (def->nsounds > 0) {
             /*
@@ -1198,8 +1197,7 @@ libxlMakeDisk(virDomainDiskDef *l_disk, libxl_device_disk *x_disk)
         return -1;
     }
 
-    if (l_disk->domain_name)
-        x_disk->backend_domname = g_strdup(l_disk->domain_name);
+    x_disk->backend_domname = g_strdup(l_disk->domain_name);
 
     return 0;
 }
diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
index 5a63efc35f..c0011e0600 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -67,8 +67,7 @@ lxcCreateFSDef(int type,
 
     def->type = type;
     def->accessmode = VIR_DOMAIN_FS_ACCESSMODE_PASSTHROUGH;
-    if (src)
-        def->src->path = g_strdup(src);
+    def->src->path = g_strdup(src);
     def->dst = g_strdup(dst);
     def->readonly = readonly;
     def->usage = usage;
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 137cb4e293..8152eea9a0 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -2960,8 +2960,7 @@ qemuMonitorJSONGetMigrationStatsReply(virJSONValue *reply,
     case QEMU_MONITOR_MIGRATION_STATUS_ERROR:
         if (error) {
             tmp = virJSONValueObjectGetString(ret, "error-desc");
-            if (tmp)
-                *error = g_strdup(tmp);
+            *error = g_strdup(tmp);
         }
         break;
 
diff --git a/src/util/virconf.c b/src/util/virconf.c
index 934632a35f..8fdf40e9d0 100644
--- a/src/util/virconf.c
+++ b/src/util/virconf.c
@@ -939,8 +939,7 @@ int virConfGetValueStringList(virConf *conf,
     case VIR_CONF_STRING:
         if (compatString) {
             *values = g_new0(char *, cval->str ? 2 : 1);
-            if (cval->str)
-                (*values)[0] = g_strdup(cval->str);
+            (*values)[0] = g_strdup(cval->str);
             break;
         }
         G_GNUC_FALLTHROUGH;
-- 
2.41.0
Re: [PATCH] src: Avoid needless checks before calling g_strdup()
Posted by Ján Tomko 7 months, 1 week ago
On a Monday in 2023, Michal Privoznik wrote:
>There are few places where the following pattern occurs:
>
>  if (var)
>      other = g_strdup(var);
>
>where @other wasn't initialized before g_strdup(). Checking for
>var != NULL is useless in this case, as that's exactly what
>g_strdup() does (in which case it returns NULL).
>
>Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
>---
> src/conf/domain_event.c      | 3 +--
> src/libxl/libxl_conf.c       | 6 ++----
> src/lxc/lxc_native.c         | 3 +--
> src/qemu/qemu_monitor_json.c | 3 +--
> src/util/virconf.c           | 3 +--
> 5 files changed, 6 insertions(+), 12 deletions(-)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano