Skip to contentSkip to conversation

glibc str functions const return fixes

PATCH In review Arnaldo Carvalho de Melo 2 participants 7 messages Dec 11, 2025 – Dec 16, 2025 perf events

lore
Share & tools
lore

Arnaldo Carvalho de Melo

author maintainer
#0 lore raw

cover glibc str functions const return fixes

Hi,

While building with Fedora 44 I noticed so interesting warnings
about touching const memory, fix some of them in this series,

- Arnaldo

Arnaldo Carvalho de Melo (4):
perf list: Remove unused 'sep' variable
perf diff: Constify strchr() return variables
perf tools: Use const for variables receiving str{str,r?chr}() returns perf trace: Don't change const char strings

 tools/perf/builtin-diff.c       | 12 +++++-------
 tools/perf/builtin-list.c       |  4 ++--
 tools/perf/builtin-trace.c      | 11 +++++++----
 tools/perf/jvmti/libjvmti.c     |  2 +-
 tools/perf/tests/parse-events.c |  4 ++--
 tools/perf/util/evlist.c        |  3 ++-
 6 files changed, 19 insertions(+), 17 deletions(-)
Signature · 1 line
2.52.0
author maintainer
#1 lore raw

PATCH 1/4 perf list: Remove unused 'sep' variable

It is just being set to the return of strchr() but never used, just ditch it and with it get rid of a warning about it not being const on fedora 44.

---
 tools/perf/builtin-list.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/builtin-list.c +2 -2
index 5cbca0bacd35237e..ac7bd0e41aa1d5d6 100644
@@ -648,7 +648,7 @@ int cmd_list(int argc, const char **argv)
 	}
 
 	for (i = 0; i < argc; ++i) {
-		char *sep, *s;
+		char *s;
 
 		if (strcmp(argv[i], "tracepoint") == 0) {
 			char *old_pmu_glob = default_ps.pmu_glob;
@@ -720,7 +720,7 @@ int cmd_list(int argc, const char **argv)
 		else if (strcmp(argv[i], "pfm") == 0)
 			print_libpfm_events(&print_cb, ps);
 #endif
-		else if ((sep = strchr(argv[i], ':')) != NULL) {
+		else if (strchr(argv[i], ':') != NULL) {
 			char *old_pmu_glob = ps->pmu_glob;
 			char *old_event_glob = ps->event_glob;
 
Signature · 1 line
2.52.0
author maintainer
#2 lore raw

PATCH 2/4 perf diff: Constify strchr() return variables

In reply to Arnaldo Carvalho de Melo

Newer glibc versions return const char for strchr() when the 's' arg is const, change the return variable to const to match that.

Also we don't need to turn that ',' into a '\0', as strtol will stop in the first invalid char. No need to touch read only memory.

First noticed with fedora 44.

---
 tools/perf/builtin-diff.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
tools/perf/builtin-diff.c +5 -7
index 53d5ea4a6a4f7b54..59bf1f72d12e226a 100644
@@ -178,10 +178,9 @@ static struct header_column {
 	}
 };
 
-static int setup_compute_opt_wdiff(char *opt)
+static int setup_compute_opt_wdiff(const char *opt)
 {
-	char *w1_str = opt;
-	char *w2_str;
+	const char *w1_str = opt, *w2_str;
 
 	int ret = -EINVAL;
 
@@ -192,8 +191,7 @@ static int setup_compute_opt_wdiff(char *opt)
 	if (!w2_str)
 		goto out;
 
-	*w2_str++ = 0x0;
-	if (!*w2_str)
+	if (!*++w2_str)
 		goto out;
 
 	compute_wdiff_w1 = strtol(w1_str, NULL, 10);
@@ -214,7 +212,7 @@ static int setup_compute_opt_wdiff(char *opt)
 	return ret;
 }
 
-static int setup_compute_opt(char *opt)
+static int setup_compute_opt(const char *opt)
 {
 	if (compute == COMPUTE_WEIGHTED_DIFF)
 		return setup_compute_opt_wdiff(opt);
@@ -234,7 +232,7 @@ static int setup_compute(const struct option *opt, const char *str,
 	char *cstr = (char *) str;
 	char buf[50];
 	unsigned i;
-	char *option;
+	const char *option;
 
 	if (!str) {
 		*cp = COMPUTE_DELTA;
Signature · 1 line
2.52.0
author maintainer
#3 lore raw

PATCH 3/4 perf tools: Use const for variables receiving str{str,r?chr}() returns

In reply to Arnaldo Carvalho de Melo

Newer glibc versions return const char for str{str,chr}() where the haystack/s is const so to avoid warnings like these on fedora 44 change some variables to const:

36 8.17 fedora:44 : FAIL gcc version 15.2.1 20251111 (Red Hat 15.2.1-4) (GCC)
libbpf.c: In function 'kallsyms_cb':
libbpf.c:8489:13: error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
8489 | res = strstr(sym_name, ".llvm.");

---
 tools/perf/jvmti/libjvmti.c     | 2 +-
 tools/perf/tests/parse-events.c | 4 ++--
 tools/perf/util/evlist.c        | 3 ++-
 3 files changed, 5 insertions(+), 4 deletions(-)
tools/perf/jvmti/libjvmti.c +1 -1
index 82514e6532b8c3af..87bfd4781003a331 100644
@@ -142,7 +142,7 @@ copy_class_filename(const char * class_sign, const char * file_name, char * resu
 	*/
 	if (*class_sign == 'L') {
 		size_t j, i = 0;
-		char *p = strrchr(class_sign, '/');
+		const char *p = strrchr(class_sign, '/');
 		if (p) {
 			/* drop the 'L' prefix and copy up to the final '/' */
 			for (i = 0; i < (size_t)(p - class_sign); i++)
tools/perf/tests/parse-events.c +2 -2
index 128d21dc389f869b..2bd6229721145e42 100644
@@ -2609,8 +2609,8 @@ static int test_events(const struct evlist_test *events, int cnt)
 	for (int i = 0; i < cnt; i++) {
 		struct evlist_test e = events[i];
 		int test_ret;
-		const char *pos = e.name;
-		char buf[1024], *buf_pos = buf, *end;
+		const char *pos = e.name, *end;
+		char buf[1024], *buf_pos = buf;
 
 		while ((end = strstr(pos, "default_core"))) {
 			size_t len = end - pos;
tools/perf/util/evlist.c +2 -1
index 03674d2cbd015e4f..64951962854104a5 100644
@@ -1945,7 +1945,8 @@ static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_
 
 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
 {
-	char *comma = NULL, *endptr = NULL;
+	const char *comma = NULL;
+	char *endptr = NULL;
 
 	*ctl_fd_close = false;
 
Signature · 1 line
2.52.0
author maintainer
#4 lore raw

PATCH 4/4 perf trace: Don't change const char strings

In reply to Arnaldo Carvalho de Melo

We got away with this so far but now with fedora 44 complaining about the return value of strchr et all, lets use strdup for good measure.

---
 tools/perf/builtin-trace.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/perf/builtin-trace.c +7 -4
index baee1f6956001d86..d49c1ae409d77df1 100644
@@ -5173,8 +5173,8 @@ static int trace__parse_events_option(const struct option *opt, const char *str,
 				      int unset __maybe_unused)
 {
 	struct trace *trace = (struct trace *)opt->value;
-	const char *s = str;
-	char *sep = NULL, *lists[2] = { NULL, NULL, };
+	const char *s;
+	char *strd, *sep = NULL, *lists[2] = { NULL, NULL, };
 	int len = strlen(str) + 1, err = -1, list, idx;
 	char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
 	char group_name[PATH_MAX];
@@ -5183,6 +5183,10 @@ static int trace__parse_events_option(const struct option *opt, const char *str,
 	if (strace_groups_dir == NULL)
 		return -1;
 
+	s = strd = strdup(str);
+	if (strd == NULL)
+		return -1;
+
 	if (*s == '!') {
 		++s;
 		trace->not_ev_qualifier = true;
@@ -5257,8 +5261,7 @@ static int trace__parse_events_option(const struct option *opt, const char *str,
 	free(strace_groups_dir);
 	free(lists[0]);
 	free(lists[1]);
-	if (sep)
-		*sep = ',';
+	free(strd);
 
 	return err;
 }
Signature · 1 line
2.52.0
reviewer Reviewed-by on the v1 series
#5 lore raw
In reply to Arnaldo Carvalho de Melo
Quoted 7 lines · Arnaldo Carvalho de Melo

Hi,

While building with Fedora 44 I noticed so interesting warnings
about touching const memory, fix some of them in this series,

- Arnaldo

For the series:

Thanks,
Ian

Quoted 18 lines

Arnaldo Carvalho de Melo (4):
perf list: Remove unused 'sep' variable
perf diff: Constify strchr() return variables
perf tools: Use const for variables receiving str{str,r?chr}() returns perf trace: Don't change const char strings

 tools/perf/builtin-diff.c       | 12 +++++-------
 tools/perf/builtin-list.c       |  4 ++--
 tools/perf/builtin-trace.c      | 11 +++++++----
 tools/perf/jvmti/libjvmti.c     |  2 +-
 tools/perf/tests/parse-events.c |  4 ++--
 tools/perf/util/evlist.c        |  3 ++-
 6 files changed, 19 insertions(+), 17 deletions(-)

--
2.52.0

author maintainer Applied
#6 lore raw
Quoted 8 lines · Ian Rogers
Quoted 2 lines · Arnaldo Carvalho de Melo

While building with Fedora 44 I noticed so interesting warnings
about touching const memory, fix some of them in this series,

For the series:

Thanks, applied to perf-tools-next,

- Arnaldo