]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
vsh: do not cast away const
authorJán Tomko <jtomko@redhat.com>
Tue, 10 Aug 2021 16:56:18 +0000 (18:56 +0200)
committerJán Tomko <jtomko@redhat.com>
Wed, 11 Aug 2021 08:52:58 +0000 (10:52 +0200)
Instead of using the same variable to store either a const pointer
or an allocated string, always make a copy.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
tools/vsh.c

index 123284c636ecca72b7ce010534fed07b77869134..3bbaecd2ea4d0e8bd429e12f1d79c2c292a99318 100644 (file)
@@ -318,7 +318,7 @@ vshCmddefCheckInternals(vshControl *ctl,
 
         case VSH_OT_ALIAS: {
             size_t j;
-            char *name = (char *)opt->help; /* cast away const */
+            g_autofree char *name = NULL;
             char *p;
 
             if (opt->flags || !opt->help) {
@@ -326,15 +326,16 @@ vshCmddefCheckInternals(vshControl *ctl,
                          opt->name, cmd->name);
                 return -1; /* alias options are tracked by the original name */
             }
-            if ((p = strchr(name, '=')))
-                name = g_strndup(name, p - name);
+            if ((p = strchr(opt->help, '=')))
+                name = g_strndup(opt->help, p - opt->help);
+            else
+                name = g_strdup(opt->help);
             for (j = i + 1; cmd->opts[j].name; j++) {
                 if (STREQ(name, cmd->opts[j].name) &&
                     cmd->opts[j].type != VSH_OT_ALIAS)
                     break;
             }
-            if (name != opt->help) {
-                VIR_FREE(name);
+            if (p) {
                 /* If alias comes with value, replacement must not be bool */
                 if (cmd->opts[j].type == VSH_OT_BOOL) {
                     vshError(ctl, _("alias '%s' of command '%s' has mismatched alias type"),