]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
lib: Avoid changing const strings via strchr() and friends
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 26 Nov 2025 13:50:11 +0000 (14:50 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 2 Dec 2025 11:42:41 +0000 (12:42 +0100)
There's new commit in glibc [1] which makes memchr(), strchr(),
strrchr(), strpbrk() and strstr() reflect type of the input
string. If it's a constant string, then the return type of these
functions is also 'const char *'. But this change tickles
-Wincompatible-pointer-types-discards-qualifiers warning.

And indeed, there are some places where we use a 'char *' typed
variable to store the retval, or even misuse the fact 'char *' is
returned and modify const string.

To fix this, a couple of different approaches is used:

  a) switch variable type to 'const char *',
  b) switch argument to 'char *' (in a few places we have
     strdup()-ed) the const string already,
  c) strdup() the string and use b).

1: https://sourceware.org/git/?p=glibc.git;a=commit;h=cd748a63ab1a7ae846175c532a3daab341c62690
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Tested-by: Jaroslav Suchanek <jsuchane@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
17 files changed:
src/interface/interface_backend_udev.c
src/libxl/xen_common.c
src/libxl/xen_xm.c
src/node_device/node_device_udev.c
src/nwfilter/nwfilter_ebiptables_driver.c
src/qemu/qemu_monitor_json.c
src/qemu/qemu_nbdkit.c
src/rpc/virnetsshsession.c
src/storage/storage_util.c
src/storage_file/storage_source.c
src/util/vircgroup.c
src/util/virfile.c
src/util/virstoragefile.c
src/util/virsysinfo.c
src/util/virxml.c
src/vmware/vmware_conf.c
tools/vsh.c

index 48eacdcdc2ba62e20572cb0b8c2d431010191785..c6d14aa8039adbc342f49d868466888a153027bb 100644 (file)
@@ -927,7 +927,7 @@ udevGetIfaceDef(struct udev *udev, const char *name)
     g_autoptr(virInterfaceDef) ifacedef = NULL;
     unsigned int mtu;
     const char *mtu_str;
-    char *vlan_parent_dev = NULL;
+    const char *vlan_parent_dev = NULL;
     const char *devtype;
 
     /* Allocate our interface definition structure */
index 666c6cae209e8d27e928263ac23c14665aacff77..890ef1172312548fee7198ebfcf9e2f1dd133691 100644 (file)
@@ -802,20 +802,20 @@ static virDomainChrDef *
 xenParseSxprChar(const char *value,
                  const char *tty)
 {
-    const char *prefix;
+    g_autofree char *prefix = NULL;
     char *tmp;
     virDomainChrDef *def;
 
     if (!(def = virDomainChrDefNew(NULL)))
         return NULL;
 
-    prefix = value;
+    prefix = g_strdup(value);
 
     if (g_path_is_absolute(value)) {
         def->source->type = VIR_DOMAIN_CHR_TYPE_DEV;
         def->source->data.file.path = g_strdup(value);
     } else {
-        if ((tmp = strchr(value, ':')) != NULL) {
+        if ((tmp = strchr(prefix, ':')) != NULL) {
             *tmp = '\0';
             value = tmp + 1;
         }
@@ -1019,7 +1019,7 @@ xenParseCharDev(virConf *conf, virDomainDef *def, const char *nativeFormat)
 static int
 xenParseVifBridge(virDomainNetDef *net, const char *bridge)
 {
-    char *vlanstr;
+    const char *vlanstr;
     unsigned int tag;
 
     if ((vlanstr = strchr(bridge, '.'))) {
@@ -1144,7 +1144,7 @@ xenParseVif(char *entry, const char *vif_typename)
 
     for (keyval = keyvals; keyval && *keyval; keyval++) {
         const char *key = *keyval;
-        char *val = strchr(key, '=');
+        const char *val = strchr(key, '=');
 
         virSkipSpaces(&key);
 
index 274b35153b182202af6a3807e4dc47c41abc122e..4327a9391d96eb8ba39b1b2318bd3da8f54ac573 100644 (file)
@@ -154,9 +154,11 @@ xenParseXMDisk(char *entry, int hvm)
     src = virDomainDiskGetSource(disk);
     if (src) {
         size_t len;
+        const char *sep;
+
         /* The main type  phy:, file:, tap: ... */
-        if ((tmp = strchr(src, ':')) != NULL) {
-            len = tmp - src;
+        if ((sep = strchr(src, ':')) != NULL) {
+            len = sep - src;
             tmp = g_strndup(src, len);
 
             virDomainDiskSetDriver(disk, tmp);
@@ -173,9 +175,9 @@ xenParseXMDisk(char *entry, int hvm)
             STREQ_NULLABLE(virDomainDiskGetDriver(disk), "tap2")) {
             char *driverType;
 
-            if (!(tmp = strchr(src, ':')))
+            if (!(sep = strchr(src, ':')))
                 goto error;
-            len = tmp - src;
+            len = sep - src;
 
             driverType = g_strndup(src, len);
 
index 27e62febe8e3a0dbf82bd1227813865fb9cc3540..20a525bcec79e273190ac80642eb2ebf6e4da739 100644 (file)
@@ -1187,7 +1187,7 @@ static int
 udevGetCCWAddress(const char *sysfs_path,
                   virNodeDevCapData *data)
 {
-    char *p;
+    const char *p;
     g_autofree virCCWDeviceAddress *ccw_addr = g_new0(virCCWDeviceAddress, 1);
 
     if ((p = strrchr(sysfs_path, '/')) == NULL ||
index 4578152670ababa22a589308a7c0dbf67212b31b..859347409c91f427f6b4daa222c757356c161a10 100644 (file)
@@ -2604,7 +2604,7 @@ ebtablesRemoveSubChainsQuery(virFirewall *fw,
     const char *chainprefixes = opaque;
 
     for (i = 0; lines[i] != NULL; i++) {
-        char *tmp = strstr(lines[i], "-j ");
+        const char *tmp = strstr(lines[i], "-j ");
 
         VIR_DEBUG("Considering '%s'", lines[i]);
 
@@ -2708,7 +2708,7 @@ ebtablesRenameTmpSubAndRootChainsQuery(virFirewall *fw,
     char newchain[MAX_CHAINNAME_LENGTH];
 
     for (i = 0; lines[i] != NULL; i++) {
-        char *tmp = strstr(lines[i], "-j ");
+        const char *tmp = strstr(lines[i], "-j ");
 
         VIR_DEBUG("Considering '%s'", lines[i]);
 
index 494d7ef515576a6de786c54abe62de8ac51f276c..a602b1e65b96d0cc63c3b6fd2d8377bad6cafa8a 100644 (file)
@@ -241,7 +241,7 @@ qemuMonitorJSONIOProcess(qemuMonitor *mon,
     /*VIR_DEBUG("Data %d bytes [%s]", len, data);*/
 
     while (used < len) {
-        char *nl = strstr(data + used, LINE_ENDING);
+        const char *nl = strstr(data + used, LINE_ENDING);
 
         if (nl) {
             int got = nl - (data + used);
index c1bc6bc363964d36ccfeb44a46a7fcdc57463b52..542a6b1f44aeccdadfdc250c729d1c63b2560b80 100644 (file)
@@ -146,7 +146,7 @@ qemuNbdkitCapsQueryBuildConfig(qemuNbdkitCaps *nbdkit)
     size_t i;
     g_autofree char *output = NULL;
     g_auto(GStrv) lines = NULL;
-    const char *line;
+    char *line;
     g_autoptr(virCommand) cmd = virCommandNewArgList(nbdkit->path,
                                                      "--dump-config",
                                                      NULL);
index 316521d4cf2beba4bab6a938ac3fa8ef0e6c51f4..83c86304807d4ff9dfbb4ef3e09068b4facd7bd0 100644 (file)
@@ -216,13 +216,14 @@ virNetSSHKbIntCb(const char *name G_GNUC_UNUSED,
 
     /* fill data structures for auth callback */
     for (i = 0; i < num_prompts; i++) {
-        askcred[i].prompt = g_strdup((char*)prompts[i].text);
+        char *prompt = g_strdup((char*)prompts[i].text);
 
         /* remove colon and trailing spaces from prompts, as default behavior
          * of libvirt's auth callback is to add them */
-        if ((tmp = strrchr(askcred[i].prompt, ':')))
+        if ((tmp = strrchr(prompt, ':')))
             *tmp = '\0';
 
+        askcred[i].prompt = prompt;
         askcred[i].type = prompts[i].echo ? credtype_echo : credtype_noecho;
     }
 
index 8f7fc6cc01e32f1429496412a6027e79a092a310..e2b41de1f239a4e20e154eb3f8b7c68534b14e53 100644 (file)
@@ -3773,7 +3773,7 @@ getOldStyleBlockDevice(const char *lun_path G_GNUC_UNUSED,
                        const char *block_name,
                        char **block_device)
 {
-    char *blockp = NULL;
+    const char *blockp = NULL;
 
     /* old-style; just parse out the sd */
     if (!(blockp = strrchr(block_name, ':'))) {
index 843910a0d833813255651e5cd14d06d5a5b7337a..1883225a8b3b8d18e1419326e609ff97798f0abb 100644 (file)
@@ -45,8 +45,8 @@ VIR_LOG_INIT("storage_source");
 static bool
 virStorageSourceBackinStoreStringIsFile(const char *backing)
 {
-    char *colon;
-    char *slash;
+    const char *colon;
+    const char *slash;
 
     if (!backing)
         return false;
index 532a7e56901195ed99c21f14b92b71e041773b3e..3d66d3acb26654d041d793302349c3a4c0e32716 100644 (file)
@@ -517,7 +517,7 @@ int
 virCgroupSetValueRaw(const char *path,
                      const char *value)
 {
-    char *tmp;
+    const char *tmp;
 
     VIR_DEBUG("Set path '%s' to value '%s'", path, value);
     if (virFileWriteStr(path, value, 0) < 0) {
index a5c9fbe0d92408996f23413327a9dbd323a97474..f195d02e29fdd0b431068888fa32e1e59bd1fdaf 100644 (file)
@@ -3359,7 +3359,7 @@ char *
 virFileSanitizePath(const char *path)
 {
     const char *cur = path;
-    char *uri;
+    const char *uri;
     char *cleanpath;
     int idx = 0;
 
index 031baa1407bde65d0e3029cdd49613b39daefa3e..22bda87643d3f1838f34ccc604723aa887c857f3 100644 (file)
@@ -119,8 +119,8 @@ virStorageFileGetNPIVKey(const char *path,
                          char **key)
 {
     int status;
-    const char *serial;
-    const char *port;
+    char *serial;
+    char *port;
     g_autofree char *outbuf = NULL;
     g_autoptr(virCommand) cmd = NULL;
 
index c017ad34b7cc53bf4730f73d553e251ea8e8c414..b638fbd16c7f8e9313cd8d928c342dec30270751 100644 (file)
@@ -219,7 +219,7 @@ static int
 virSysinfoParsePPCSystem(const char *base, virSysinfoSystemDef **sysdef)
 {
     int ret = -1;
-    char *eol = NULL;
+    const char *eol = NULL;
     const char *cur;
     virSysinfoSystemDef *def;
 
@@ -267,7 +267,7 @@ static void
 virSysinfoParsePPCProcessor(const char *base, virSysinfoDef *ret)
 {
     const char *cur;
-    char *eol, *tmp_base;
+    const char *eol, *tmp_base;
     virSysinfoProcessorDef *processor;
 
     while ((tmp_base = strstr(base, "processor")) != NULL) {
@@ -336,7 +336,7 @@ static int
 virSysinfoParseARMSystem(const char *base, virSysinfoSystemDef **sysdef)
 {
     int ret = -1;
-    char *eol = NULL;
+    const char *eol = NULL;
     const char *cur;
     virSysinfoSystemDef *def;
 
@@ -384,7 +384,7 @@ static void
 virSysinfoParseARMProcessor(const char *base, virSysinfoDef *ret)
 {
     const char *cur;
-    char *eol, *tmp_base;
+    const char *eol, *tmp_base;
     virSysinfoProcessorDef *processor;
     char *processor_type = NULL;
 
index 77c7b5a8f45c076f76716ef6dd9f9f057318adc6..274f0725981b5b9dadc935b4727398006911b277 100644 (file)
@@ -368,7 +368,8 @@ virXMLCheckIllegalChars(const char *nodeName,
                         const char *str,
                         const char *illegal)
 {
-    char *c;
+    const char *c;
+
     if ((c = strpbrk(str, illegal))) {
         virReportError(VIR_ERR_XML_DETAIL,
                        _("invalid char in %1$s: %2$c"), nodeName, *c);
index a598b512dcd45f9b9e3da8934c5c37e1fb4a4d3a..11db044b520324ab43858911fb1bd9bd1b1ec869 100644 (file)
@@ -305,7 +305,7 @@ vmwareDomainConfigDisplay(vmwareDomainPtr pDomain, virDomainDef *def)
 static int
 vmwareParsePath(const char *path, char **directory, char **filename)
 {
-    char *separator;
+    const char *separator;
 
     separator = strrchr(path, '/');
 
index 4aacc5feacc63a9730527b75b0871818b7696e42..69d3930e43f62922f568f9c9733e15612aa93929 100644 (file)
@@ -413,7 +413,7 @@ vshCmddefCheckInternals(vshControl *ctl,
         case VSH_OT_ALIAS: {
             size_t j;
             g_autofree char *name = NULL;
-            char *p;
+            const char *p;
 
             if (opt->required ||
                 opt->positional ||
@@ -502,7 +502,7 @@ vshCmdGetOption(vshControl *ctl,
                alias of option and its default value */
             alias = g_strdup(n->def->help);
             name = alias;
-            if ((value = strchr(name, '='))) {
+            if ((value = strchr(alias, '='))) {
                 *value = '\0';
                 if (*optstr) {
                     if (report)
@@ -1660,7 +1660,7 @@ vshCommandParse(vshControl *ctl,
              *   value
              *   -- (terminate accepting '--option', fill only positional args)
              */
-            const char *optionname = tkdata + 2;
+            char *optionname = tkdata + 2;
             char *sep;
 
             if (!STRPREFIX(tkdata, "--")) {