From: Michal Privoznik Date: Wed, 26 Nov 2025 13:50:11 +0000 (+0100) Subject: lib: Avoid changing const strings via strchr() and friends X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=414c5b11bffedbe4c54778376a701360428358b0;p=thirdparty%2Flibvirt.git lib: Avoid changing const strings via strchr() and friends 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 Tested-by: Jaroslav Suchanek Reviewed-by: Ján Tomko --- diff --git a/src/interface/interface_backend_udev.c b/src/interface/interface_backend_udev.c index 48eacdcdc2..c6d14aa803 100644 --- a/src/interface/interface_backend_udev.c +++ b/src/interface/interface_backend_udev.c @@ -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 */ diff --git a/src/libxl/xen_common.c b/src/libxl/xen_common.c index 666c6cae20..890ef11723 100644 --- a/src/libxl/xen_common.c +++ b/src/libxl/xen_common.c @@ -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); diff --git a/src/libxl/xen_xm.c b/src/libxl/xen_xm.c index 274b35153b..4327a9391d 100644 --- a/src/libxl/xen_xm.c +++ b/src/libxl/xen_xm.c @@ -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); diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c index 27e62febe8..20a525bcec 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -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 || diff --git a/src/nwfilter/nwfilter_ebiptables_driver.c b/src/nwfilter/nwfilter_ebiptables_driver.c index 4578152670..859347409c 100644 --- a/src/nwfilter/nwfilter_ebiptables_driver.c +++ b/src/nwfilter/nwfilter_ebiptables_driver.c @@ -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]); diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 494d7ef515..a602b1e65b 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -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); diff --git a/src/qemu/qemu_nbdkit.c b/src/qemu/qemu_nbdkit.c index c1bc6bc363..542a6b1f44 100644 --- a/src/qemu/qemu_nbdkit.c +++ b/src/qemu/qemu_nbdkit.c @@ -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); diff --git a/src/rpc/virnetsshsession.c b/src/rpc/virnetsshsession.c index 316521d4cf..83c8630480 100644 --- a/src/rpc/virnetsshsession.c +++ b/src/rpc/virnetsshsession.c @@ -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; } diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c index 8f7fc6cc01..e2b41de1f2 100644 --- a/src/storage/storage_util.c +++ b/src/storage/storage_util.c @@ -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, ':'))) { diff --git a/src/storage_file/storage_source.c b/src/storage_file/storage_source.c index 843910a0d8..1883225a8b 100644 --- a/src/storage_file/storage_source.c +++ b/src/storage_file/storage_source.c @@ -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; diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 532a7e5690..3d66d3acb2 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -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) { diff --git a/src/util/virfile.c b/src/util/virfile.c index a5c9fbe0d9..f195d02e29 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3359,7 +3359,7 @@ char * virFileSanitizePath(const char *path) { const char *cur = path; - char *uri; + const char *uri; char *cleanpath; int idx = 0; diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index 031baa1407..22bda87643 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -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; diff --git a/src/util/virsysinfo.c b/src/util/virsysinfo.c index c017ad34b7..b638fbd16c 100644 --- a/src/util/virsysinfo.c +++ b/src/util/virsysinfo.c @@ -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; diff --git a/src/util/virxml.c b/src/util/virxml.c index 77c7b5a8f4..274f072598 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -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); diff --git a/src/vmware/vmware_conf.c b/src/vmware/vmware_conf.c index a598b512dc..11db044b52 100644 --- a/src/vmware/vmware_conf.c +++ b/src/vmware/vmware_conf.c @@ -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, '/'); diff --git a/tools/vsh.c b/tools/vsh.c index 4aacc5feac..69d3930e43 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -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, "--")) {