[PATCH] lib: Use NULLSTR family of macros more

Michal Privoznik posted 1 patch 3 months ago
src/libvirt.c                  | 2 +-
src/network/network_iptables.c | 4 ++--
src/network/network_nftables.c | 4 ++--
src/qemu/qemu_firmware.c       | 2 +-
src/util/virnetdevip.c         | 8 ++++----
tools/virsh-checkpoint.c       | 2 +-
tools/virsh-domain-monitor.c   | 8 ++++----
tools/virsh-nodedev.c          | 2 +-
8 files changed, 16 insertions(+), 16 deletions(-)
[PATCH] lib: Use NULLSTR family of macros more
Posted by Michal Privoznik 3 months ago
There is a family of convenient macros: NULLSTR, NULLSTR_EMPTY,
NULLSTR_STAR, NULLSTR_MINUS which hides ternary operator.

Generated using the following spatch (and its obvious variants):

  @@
  expression s;
  @@
    <+...
  - s ? s : "<null>"
  + NULLSTR(s)
    ...+>

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/libvirt.c                  | 2 +-
 src/network/network_iptables.c | 4 ++--
 src/network/network_nftables.c | 4 ++--
 src/qemu/qemu_firmware.c       | 2 +-
 src/util/virnetdevip.c         | 8 ++++----
 tools/virsh-checkpoint.c       | 2 +-
 tools/virsh-domain-monitor.c   | 8 ++++----
 tools/virsh-nodedev.c          | 2 +-
 8 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/src/libvirt.c b/src/libvirt.c
index 26c3fe454f..1d37696d6f 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -168,7 +168,7 @@ virConnectAuthCallbackDefault(virConnectCredentialPtr cred,
         }
 
         if (cred[i].type != VIR_CRED_EXTERNAL) {
-            cred[i].result = bufptr ? bufptr : g_strdup(cred[i].defresult ? cred[i].defresult : "");
+            cred[i].result = bufptr ? bufptr : g_strdup(NULLSTR_EMPTY(cred[i].defresult));
             cred[i].resultlen = strlen(cred[i].result);
         }
     }
diff --git a/src/network/network_iptables.c b/src/network/network_iptables.c
index f774176b3d..e8da15426e 100644
--- a/src/network/network_iptables.c
+++ b/src/network/network_iptables.c
@@ -867,10 +867,10 @@ iptablesForwardMasquerade(virFirewall *fw,
     if (addrStartStr && addrStartStr[0]) {
         if (addrEndStr && addrEndStr[0]) {
             natRangeStr = g_strdup_printf("%s-%s%s", addrStartStr, addrEndStr,
-                                          portRangeStr ? portRangeStr : "");
+                                          NULLSTR_EMPTY(portRangeStr));
         } else {
             natRangeStr = g_strdup_printf("%s%s", addrStartStr,
-                                          portRangeStr ? portRangeStr : "");
+                                          NULLSTR_EMPTY(portRangeStr));
         }
 
         virFirewallCmdAddArgList(fw, fwCmd,
diff --git a/src/network/network_nftables.c b/src/network/network_nftables.c
index 268d1f12ca..f8b5ab665d 100644
--- a/src/network/network_nftables.c
+++ b/src/network/network_nftables.c
@@ -588,10 +588,10 @@ nftablesAddForwardMasquerade(virFirewall *fw,
     if (addrStartStr && addrStartStr[0]) {
         if (addrEndStr && addrEndStr[0]) {
             natRangeStr = g_strdup_printf("%s-%s%s", addrStartStr, addrEndStr,
-                                          portRangeStr ? portRangeStr : "");
+                                          NULLSTR_EMPTY(portRangeStr));
         } else {
             natRangeStr = g_strdup_printf("%s%s", addrStartStr,
-                                          portRangeStr ? portRangeStr : "");
+                                          NULLSTR_EMPTY(portRangeStr));
         }
 
         virFirewallCmdAddArgList(fw, fwCmd, "counter", "snat", "to", natRangeStr, NULL);
diff --git a/src/qemu/qemu_firmware.c b/src/qemu/qemu_firmware.c
index 08ca99e1ac..914f8596bd 100644
--- a/src/qemu/qemu_firmware.c
+++ b/src/qemu/qemu_firmware.c
@@ -1090,7 +1090,7 @@ qemuFirmwareEnsureNVRAM(virDomainDef *def,
 
     loader->nvram->path = g_strdup_printf("%s/%s_VARS%s",
                                           cfg->nvramDir, def->name,
-                                          ext ? ext : "");
+                                          NULLSTR_EMPTY(ext));
 }
 
 
diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
index 3b02fe0cec..f365c1999c 100644
--- a/src/util/virnetdevip.c
+++ b/src/util/virnetdevip.c
@@ -188,8 +188,8 @@ virNetDevIPAddrAdd(const char *ifname,
 
     VIR_DEBUG("Adding IP address %s/%d%s%s%s%s to %s",
               NULLSTR(ipStr), prefix,
-              peerStr ? " peer " : "", peerStr ? peerStr : "",
-              bcastStr ? " bcast " : "", bcastStr ? bcastStr : "",
+              peerStr ? " peer " : "", NULLSTR_EMPTY(peerStr),
+              bcastStr ? " bcast " : "", NULLSTR_EMPTY(bcastStr),
               ifname);
 
     if (!(nlmsg = virNetDevCreateNetlinkAddressMessage(RTM_NEWADDR, ifname,
@@ -206,8 +206,8 @@ virNetDevIPAddrAdd(const char *ifname,
         virReportError(VIR_ERR_SYSTEM_ERROR,
                        _("Failed to add IP address %1$s/%2$d%3$s%4$s%5$s%6$s to %7$s"),
                        ipStr, prefix,
-                       peerStr ? " peer " : "", peerStr ? peerStr : "",
-                       bcastStr ? " bcast " : "", bcastStr ? bcastStr : "",
+                       peerStr ? " peer " : "", NULLSTR_EMPTY(peerStr),
+                       bcastStr ? " bcast " : "", NULLSTR_EMPTY(bcastStr),
                        ifname);
         return -1;
     }
diff --git a/tools/virsh-checkpoint.c b/tools/virsh-checkpoint.c
index 972b2f979c..a0ac0d7f71 100644
--- a/tools/virsh-checkpoint.c
+++ b/tools/virsh-checkpoint.c
@@ -455,7 +455,7 @@ cmdCheckpointInfo(vshControl *ctl,
                  _("unexpected problem querying checkpoint state"));
         return false;
     }
-    vshPrint(ctl, "%-15s %s\n", _("Parent:"), parent ? parent : "-");
+    vshPrint(ctl, "%-15s %s\n", _("Parent:"), NULLSTR_MINUS(parent));
 
     /* Children, Descendants.  */
     flags = 0;
diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 74deca2f94..37184baa69 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -712,11 +712,11 @@ cmdDomiflist(vshControl *ctl, const vshCmd *cmd)
         mac = virXPathString("string(./mac/@address)", ctxt);
 
         if (vshTableRowAppend(table,
-                              target ? target : "-",
+                              NULLSTR_MINUS(target),
                               type,
-                              source ? source : "-",
-                              model ? model : "-",
-                              mac ? mac : "-",
+                              NULLSTR_MINUS(source),
+                              NULLSTR_MINUS(model),
+                              NULLSTR_MINUS(mac),
                               NULL) < 0)
             return false;
     }
diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c
index 910eaefc9e..145faff3e7 100644
--- a/tools/virsh-nodedev.c
+++ b/tools/virsh-nodedev.c
@@ -1233,7 +1233,7 @@ cmdNodeDeviceInfo(vshControl *ctl, const vshCmd *cmd)
 
     parent = virNodeDeviceGetParent(device);
     vshPrint(ctl, "%-15s %s\n", _("Name:"), virNodeDeviceGetName(device));
-    vshPrint(ctl, "%-15s %s\n", _("Parent:"), parent ? parent : "");
+    vshPrint(ctl, "%-15s %s\n", _("Parent:"), NULLSTR_EMPTY(parent));
     vshPrint(ctl, "%-15s %s\n", _("Active:"), virNodeDeviceIsActive(device) ?
              _("yes") : _("no"));
     vshPrint(ctl, "%-15s %s\n", _("Persistent:"),
-- 
2.44.2
Re: [PATCH] lib: Use NULLSTR family of macros more
Posted by Peter Krempa 3 months ago
On Mon, Aug 19, 2024 at 12:46:18 +0200, Michal Privoznik wrote:
> There is a family of convenient macros: NULLSTR, NULLSTR_EMPTY,
> NULLSTR_STAR, NULLSTR_MINUS which hides ternary operator.
> 
> Generated using the following spatch (and its obvious variants):
> 
>   @@
>   expression s;
>   @@
>     <+...
>   - s ? s : "<null>"
>   + NULLSTR(s)
>     ...+>
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/libvirt.c                  | 2 +-
>  src/network/network_iptables.c | 4 ++--
>  src/network/network_nftables.c | 4 ++--
>  src/qemu/qemu_firmware.c       | 2 +-
>  src/util/virnetdevip.c         | 8 ++++----
>  tools/virsh-checkpoint.c       | 2 +-
>  tools/virsh-domain-monitor.c   | 8 ++++----
>  tools/virsh-nodedev.c          | 2 +-
>  8 files changed, 16 insertions(+), 16 deletions(-)
> 
> diff --git a/src/libvirt.c b/src/libvirt.c
> index 26c3fe454f..1d37696d6f 100644
> --- a/src/libvirt.c
> +++ b/src/libvirt.c
> @@ -168,7 +168,7 @@ virConnectAuthCallbackDefault(virConnectCredentialPtr cred,
>          }
>  
>          if (cred[i].type != VIR_CRED_EXTERNAL) {
> -            cred[i].result = bufptr ? bufptr : g_strdup(cred[i].defresult ? cred[i].defresult : "");

oof, double eww!

> +            cred[i].result = bufptr ? bufptr : g_strdup(NULLSTR_EMPTY(cred[i].defresult));

but getting rid of this too would be out of scope

>              cred[i].resultlen = strlen(cred[i].result);
>          }
>      }


Reviewed-by: Peter Krempa <pkrempa@redhat.com>