From cb9d1609da6e623158ba5a8cb4a2712bcea4f57f Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Wed, 12 Oct 2022 11:07:24 +0100 Subject: [PATCH] sim/cgen: mask uninitialized variable warning in cgen-run.c MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit I see an uninitialized variable warning (with gcc 9.3.1) from cgen-run.c, like this: /tmp/build/sim/../../src/sim/cris/../common/cgen-run.c: In function ‘sim_resume’: /tmp/build/sim/../../src/sim/cris/../common/cgen-run.c:259:5: warning: ‘engine_fns$’ may be used uninitialized in this function [-Wmaybe-uninitialized] 259 | (* engine_fns[next_cpu_nr]) (cpu); | ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /tmp/build/sim/../../src/sim/cris/../common/cgen-run.c:232:14: note: ‘engine_fns$’ was declared here 232 | ENGINE_FN *engine_fns[MAX_NR_PROCESSORS]; | ^~~~~~~~~~ This is a false positive - we over allocate engine_fn, and then only initialize the nr_cpus entries which we will later go on to use. However, we can easily silence this warning by initializing the unused entries in engine_fns to NULL, this might also help if anyone ever looks at engine_fns in a debugger, it should now be obvious which entries are in use, and which are not. With this change the warning is gone. There should be no change in behaviour with this commit. --- sim/common/cgen-run.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sim/common/cgen-run.c b/sim/common/cgen-run.c index 9a13b0ca416..a9a493c01b9 100644 --- a/sim/common/cgen-run.c +++ b/sim/common/cgen-run.c @@ -242,6 +242,11 @@ engine_run_n (SIM_DESC sd, int next_cpu_nr, int nr_cpus, int max_insns, int fast prime_cpu (cpu, max_insns); } + /* Ensure the remaining engine_fns slots are initialized, this silences a + compiler warning when engine_fns is used below. */ + for (i = nr_cpus; i < MAX_NR_PROCESSORS; ++i) + engine_fns[i] = NULL; + while (1) { SIM_ENGINE_PREFIX_HOOK (sd); -- 2.30.2