]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
iptablesPrivateChainCreate: Avoid modifying const string
authorMichal Privoznik <mprivozn@redhat.com>
Thu, 27 Nov 2025 11:23:46 +0000 (12:23 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 2 Dec 2025 11:42:24 +0000 (12:42 +0100)
The iptablesPrivateChainCreate() function is given an array of
const strings. This constitutes a promise to the caller that the
data is not modified. But inside the data is modified anyway (to
cut out some parts of the data). Well, with a help from
g_strdup() the promise can be kept.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Tested-by: Jaroslav Suchanek <jsuchane@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/network/network_iptables.c

index 19dcfc7c8b0979d1783db0978729af8b0e4411d6..d21ce59b702d3bcc726859d546499cc9bb0b5b2f 100644 (file)
@@ -84,7 +84,7 @@ iptablesPrivateChainCreate(virFirewall *fw,
 {
     iptablesGlobalChainData *data = opaque;
     g_autoptr(GHashTable) chains = virHashNew(NULL);
-    g_autoptr(GHashTable) links = virHashNew(NULL);
+    g_autoptr(GHashTable) links = virHashNew(g_free);
     const char *const *line;
     size_t i;
 
@@ -96,16 +96,18 @@ iptablesPrivateChainCreate(virFirewall *fw,
             if (virHashUpdateEntry(chains, tmp, (void *)0x1) < 0)
                 return -1;
         } else if ((tmp = STRSKIP(*line, "-A "))) { /* eg "-A INPUT -j LIBVIRT_INP" */
-            char *sep = strchr(tmp, ' ');
+            const char *sep = strchr(tmp, ' ');
 
             if (sep) {
-                char *target;
+                const char *target;
 
-                *sep = '\0';
                 if ((target = STRSKIP(sep + 1, "-j "))) {
-                    if (virHashUpdateEntry(links, target,
-                                           (char *)tmp) < 0)
+                    char *chain = g_strndup(tmp, sep - tmp);
+
+                    if (virHashUpdateEntry(links, target, chain) < 0) {
+                        g_free(chain);
                         return -1;
+                    }
                 }
             }
         }