Skip to contentSkip to conversation

perf annotate: Initialize 'arch' variable not to trip some -Werror=maybe-uninitialized

PATCH In review Arnaldo Carvalho de Melo 3 participants 6 messages Apr 3, 2024 perf events

lore
Share & tools
lore

Arnaldo Carvalho de Melo

author maintainer
#0 lore raw

PATCH 1/1 perf annotate: Initialize 'arch' variable not to trip some -Werror=maybe-uninitialized

In some older distros the build is failing due to -Werror=maybe-uninitialized, in this case we know that this isn't the case because 'arch' gets initialized by evsel__get_arch(), so just init it to NULL to silence those cases.

E.g.:

32 17.12 opensuse:15.5 : FAIL gcc version 7.5.0 (SUSE Linux)
util/annotate.c: In function 'hist_entry__get_data_type':
util/annotate.c:2269:15: error: 'arch' may be used uninitialized in this function [-Werror=maybe-uninitialized]
struct arch *arch;
^~~~
cc1: all warnings being treated as errors

43 7.30 ubuntu:18.04-x-powerpc64el : FAIL gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
util/annotate.c: In function 'hist_entry__get_data_type':
util/annotate.c:2351:36: error: 'arch' may be used uninitialized in this function [-Werror=maybe-uninitialized]
if (map__dso(ms->map)->kernel && arch__is(arch, "x86") &&
^~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

Cc Adrian Hunter <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/perf/util/annotate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/annotate.c +1 -1
index b795f27f26024f35..f316e0b65897957a 100644
@@ -2266,7 +2266,7 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he)
 {
 	struct map_symbol *ms = &he->ms;
 	struct evsel *evsel = hists_to_evsel(he->hists);
-	struct arch *arch;
+	struct arch *arch = NULL;
 	struct disasm_line *dl;
 	struct annotated_insn_loc loc;
 	struct annotated_op_loc *op_loc;
Signature · 1 line
2.44.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 22 lines · Arnaldo Carvalho de Melo

In some older distros the build is failing due to -Werror=maybe-uninitialized, in this case we know that this isn't the case because 'arch' gets initialized by evsel__get_arch(), so just init it to NULL to silence those cases.

E.g.:

32 17.12 opensuse:15.5 : FAIL gcc version 7.5.0 (SUSE Linux)
util/annotate.c: In function 'hist_entry__get_data_type':
util/annotate.c:2269:15: error: 'arch' may be used uninitialized in this function [-Werror=maybe-uninitialized]
struct arch *arch;
^~~~
cc1: all warnings being treated as errors

43 7.30 ubuntu:18.04-x-powerpc64el : FAIL gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
util/annotate.c: In function 'hist_entry__get_data_type':
util/annotate.c:2351:36: error: 'arch' may be used uninitialized in this function [-Werror=maybe-uninitialized]
if (map__dso(ms->map)->kernel && arch__is(arch, "x86") &&
^~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

Quoted 5 lines
Cc Adrian Hunter <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>

This looks fine but I couldn't line up the errors with code in the tree. I was curious why the "maybe-uninitialized" was failing. Perhaps evsel__get_arch should set the out argument to NULL when an error occurs. This fix is also good but may potentially need repeating for other evsel__get_arch cases, so a fix in evsel__get_arch may be preferable.

Thanks,
Ian

Quoted 20 lines
---
 tools/perf/util/annotate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/annotate.c +1 -2
index b795f27f26024f35..f316e0b65897957a 100644
@@ -2266,7 +2266,7 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he)
 {
        struct map_symbol *ms = &he->ms;
        struct evsel *evsel = hists_to_evsel(he->hists);
-       struct arch *arch;
+       struct arch *arch = NULL;
        struct disasm_line *dl;
        struct annotated_insn_loc loc;
        struct annotated_op_loc *op_loc;
--

2.44.0

author maintainer
#2 lore raw
Quoted 29 lines · Ian Rogers
Quoted 22 lines · Arnaldo Carvalho de Melo

In some older distros the build is failing due to -Werror=maybe-uninitialized, in this case we know that this isn't the case because 'arch' gets initialized by evsel__get_arch(), so just init it to NULL to silence those cases.

E.g.:

32 17.12 opensuse:15.5 : FAIL gcc version 7.5.0 (SUSE Linux)
util/annotate.c: In function 'hist_entry__get_data_type':
util/annotate.c:2269:15: error: 'arch' may be used uninitialized in this function [-Werror=maybe-uninitialized]
struct arch *arch;
^~~~
cc1: all warnings being treated as errors

43 7.30 ubuntu:18.04-x-powerpc64el : FAIL gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
util/annotate.c: In function 'hist_entry__get_data_type':
util/annotate.c:2351:36: error: 'arch' may be used uninitialized in this function [-Werror=maybe-uninitialized]
if (map__dso(ms->map)->kernel && arch__is(arch, "x86") &&
^~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

Quoted 5 lines
Cc Adrian Hunter <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Quoted 8 lines

This looks fine but I couldn't line up the errors with code in the tree. I was curious why the "maybe-uninitialized" was failing. Perhaps evsel__get_arch should set the out argument to NULL when an error occurs. This fix is also good but may potentially need repeating for other evsel__get_arch cases, so a fix in evsel__get_arch may be preferable.

Trying with this on top, i.e. what you suggests:

From 70e6fd996ce7f9c9adbb30640ed666025bf6f1e4 Mon Sep 17 00:00:00 2001
From: Arnaldo Carvalho de Melo <[email protected]>
Date: Wed, 3 Apr 2024 16:49:54 -0300
Subject: [PATCH 1/1] WIP

---
 tools/perf/util/annotate.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/util/annotate.c +4 -2
index f316e0b65897957a..35235147b111e788 100644
@@ -838,8 +838,10 @@ static int evsel__get_arch(struct evsel *evsel, struct arch **parch)
 	struct arch *arch;
 	int err;
 
-	if (!arch_name)
+	if (!arch_name) {
+		*parch = NULL;
 		return errno;
+	}
 
 	*parch = arch = arch__find(arch_name);
 	if (arch == NULL) {
@@ -2266,7 +2268,7 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he)
 {
 	struct map_symbol *ms = &he->ms;
 	struct evsel *evsel = hists_to_evsel(he->hists);
-	struct arch *arch = NULL;
+	struct arch *arch;
 	struct disasm_line *dl;
 	struct annotated_insn_loc loc;
 	struct annotated_op_loc *op_loc;
Signature · 1 line
2.44.0
maintainer Acked-by
#3 lore raw
Quoted 40 lines · Arnaldo Carvalho de Melo
Quoted 29 lines · Ian Rogers
Quoted 22 lines · Arnaldo Carvalho de Melo

In some older distros the build is failing due to -Werror=maybe-uninitialized, in this case we know that this isn't the case because 'arch' gets initialized by evsel__get_arch(), so just init it to NULL to silence those cases.

E.g.:

32 17.12 opensuse:15.5 : FAIL gcc version 7.5.0 (SUSE Linux)
util/annotate.c: In function 'hist_entry__get_data_type':
util/annotate.c:2269:15: error: 'arch' may be used uninitialized in this function [-Werror=maybe-uninitialized]
struct arch *arch;
^~~~
cc1: all warnings being treated as errors

43 7.30 ubuntu:18.04-x-powerpc64el : FAIL gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
util/annotate.c: In function 'hist_entry__get_data_type':
util/annotate.c:2351:36: error: 'arch' may be used uninitialized in this function [-Werror=maybe-uninitialized]
if (map__dso(ms->map)->kernel && arch__is(arch, "x86") &&
^~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

Quoted 5 lines
Cc Adrian Hunter <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Quoted 8 lines

This looks fine but I couldn't line up the errors with code in the tree. I was curious why the "maybe-uninitialized" was failing. Perhaps evsel__get_arch should set the out argument to NULL when an error occurs. This fix is also good but may potentially need repeating for other evsel__get_arch cases, so a fix in evsel__get_arch may be preferable.

Quoted 8 lines

Trying with this on top, i.e. what you suggests:

From 70e6fd996ce7f9c9adbb30640ed666025bf6f1e4 Mon Sep 17 00:00:00 2001
From: Arnaldo Carvalho de Melo <[email protected]>
Date: Wed, 3 Apr 2024 16:49:54 -0300
Subject: [PATCH 1/1] WIP

Thanks,
Namhyung

Quoted 32 lines
---
 tools/perf/util/annotate.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/util/annotate.c +4 -3
index f316e0b65897957a..35235147b111e788 100644
@@ -838,8 +838,10 @@ static int evsel__get_arch(struct evsel *evsel, struct arch **parch)
        struct arch *arch;
        int err;
 
-       if (!arch_name)
+       if (!arch_name) {
+               *parch = NULL;
                return errno;
+       }
 
        *parch = arch = arch__find(arch_name);
        if (arch == NULL) {
@@ -2266,7 +2268,7 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he)
 {
        struct map_symbol *ms = &he->ms;
        struct evsel *evsel = hists_to_evsel(he->hists);
-       struct arch *arch = NULL;
+       struct arch *arch;
        struct disasm_line *dl;
        struct annotated_insn_loc loc;
        struct annotated_op_loc *op_loc;
--

2.44.0

author maintainer
#4 lore raw
In reply to Ian Rogers
Quoted 29 lines · Ian Rogers
Quoted 22 lines · Arnaldo Carvalho de Melo

In some older distros the build is failing due to -Werror=maybe-uninitialized, in this case we know that this isn't the case because 'arch' gets initialized by evsel__get_arch(), so just init it to NULL to silence those cases.

E.g.:

32 17.12 opensuse:15.5 : FAIL gcc version 7.5.0 (SUSE Linux)
util/annotate.c: In function 'hist_entry__get_data_type':
util/annotate.c:2269:15: error: 'arch' may be used uninitialized in this function [-Werror=maybe-uninitialized]
struct arch *arch;
^~~~
cc1: all warnings being treated as errors

43 7.30 ubuntu:18.04-x-powerpc64el : FAIL gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
util/annotate.c: In function 'hist_entry__get_data_type':
util/annotate.c:2351:36: error: 'arch' may be used uninitialized in this function [-Werror=maybe-uninitialized]
if (map__dso(ms->map)->kernel && arch__is(arch, "x86") &&
^~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

Quoted 5 lines
Cc Adrian Hunter <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Quoted 8 lines

This looks fine but I couldn't line up the errors with code in the tree. I was curious why the "maybe-uninitialized" was failing. Perhaps evsel__get_arch should set the out argument to NULL when an error occurs. This fix is also good but may potentially need repeating for other evsel__get_arch cases, so a fix in evsel__get_arch may be preferable.

Yeah, your suggestion is better and I just tested, satisfies the compilers that were emitting this warning.

I stamped a:

and kept your Reviewed-by, ok?

- Arnaldo

reviewer
#5 lore raw
Quoted 40 lines · Arnaldo Carvalho de Melo
Quoted 29 lines · Ian Rogers
Quoted 22 lines · Arnaldo Carvalho de Melo

In some older distros the build is failing due to -Werror=maybe-uninitialized, in this case we know that this isn't the case because 'arch' gets initialized by evsel__get_arch(), so just init it to NULL to silence those cases.

E.g.:

32 17.12 opensuse:15.5 : FAIL gcc version 7.5.0 (SUSE Linux)
util/annotate.c: In function 'hist_entry__get_data_type':
util/annotate.c:2269:15: error: 'arch' may be used uninitialized in this function [-Werror=maybe-uninitialized]
struct arch *arch;
^~~~
cc1: all warnings being treated as errors

43 7.30 ubuntu:18.04-x-powerpc64el : FAIL gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
util/annotate.c: In function 'hist_entry__get_data_type':
util/annotate.c:2351:36: error: 'arch' may be used uninitialized in this function [-Werror=maybe-uninitialized]
if (map__dso(ms->map)->kernel && arch__is(arch, "x86") &&
^~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

Quoted 5 lines
Cc Adrian Hunter <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Quoted 8 lines

This looks fine but I couldn't line up the errors with code in the tree. I was curious why the "maybe-uninitialized" was failing. Perhaps evsel__get_arch should set the out argument to NULL when an error occurs. This fix is also good but may potentially need repeating for other evsel__get_arch cases, so a fix in evsel__get_arch may be preferable.

Quoted 8 lines

Yeah, your suggestion is better and I just tested, satisfies the compilers that were emitting this warning.

I stamped a:

and kept your Reviewed-by, ok?

Sure :-)

Thanks,
Ian

Quoted 1 line

- Arnaldo