Skip to contentSkip to conversation

lib: sbi: Resolve the uninitialized complaint in sbi_pmu

PATCH New Dong Du 3 participants 5 messages Nov 25, 2021 – Nov 30, 2021

lore
Share & tools
lore

Dong Du

author
#0 lore raw

PATCH 1/1 lib: sbi: Resolve the uninitialized complaint in sbi_pmu

A recent commit (b28f0700) turns to read from CSR_MCOUNTINHIBIT after checking the SBI_HART_HAS_MCOUNTINHIBIT HART feature, which leaves the mctr_inhbt uninitialized in pmu_ctr_find_hw().
As a result, compiler will complain:
error: 'mctr_inhbt' may be used uninitialized in this function.

This commit resolves the issue by assigning an initial value to mctr_inhbt.

---
 lib/sbi/sbi_pmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +1 -1
index a0eb359..5950a20 100644
@@ -489,7 +489,7 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo
 	unsigned long ctr_mask;
 	int i, ret = 0, fixed_ctr, ctr_idx = SBI_ENOTSUPP;
 	struct sbi_pmu_hw_event *temp;
-	unsigned long mctr_inhbt;
+	unsigned long mctr_inhbt = 0;
 	u32 hartid = current_hartid();
 	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
 
Signature · 1 line
2.33.1
#1 lore raw

PATCH 1/1 lib: sbi: Resolve the uninitialized complaint in sbi_pmu

? 2021-11-25???? 18:38 +0800?Dong Du???

Quoted 11 lines

A recent commit (b28f0700) turns to read from CSR_MCOUNTINHIBIT after checking the SBI_HART_HAS_MCOUNTINHIBIT HART feature, which leaves the
mctr_inhbt uninitialized in pmu_ctr_find_hw().
As a result, compiler will complain:
error: 'mctr_inhbt' may be used uninitialized in this function.

This commit resolves the issue by assigning an initial value to mctr_inhbt.

SBI_HART_HAS_MCOUNTINHIBIT has been tested before accessing mctr_inhbt, so there is no need to initialize

Regards,
Xiang W

Quoted 22 lines

---
?lib/sbi/sbi_pmu.c | 2 +-
?1 file changed, 1 insertion(+), 1 deletion(-)

lib/sbi/sbi_pmu.c +0 -0
index a0eb359..5950a20 100644
@@ -489,7 +489,7 @@ static int pmu_ctr_find_hw(unsigned long cbase,

unsigned long cmask, unsigned lo
????????unsigned long ctr_mask;
????????int i, ret = 0, fixed_ctr, ctr_idx = SBI_ENOTSUPP; ????????struct sbi_pmu_hw_event *temp;
-???????unsigned long mctr_inhbt;
+???????unsigned long mctr_inhbt = 0;
????????u32 hartid = current_hartid();
????????struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); ?

Signature · 1 line
2.33.1
author
#2 lore raw

PATCH 1/1 lib: sbi: Resolve the uninitialized complaint in sbi_pmu

Quoted 8 lines · On Nov 25, 2021, at 11:35 PM, Xiang wxjstz at 126.com wrote:

? 2021-11-25???? 18:38 +0800?Dong Du???

Quoted 7 lines

A recent commit (b28f0700) turns to read from CSR_MCOUNTINHIBIT after checking the SBI_HART_HAS_MCOUNTINHIBIT HART feature, which leaves the
mctr_inhbt uninitialized in pmu_ctr_find_hw().
As a result, compiler will complain:
error: 'mctr_inhbt' may be used uninitialized in this function.

Quoted 5 lines
Quoted 4 lines

This commit resolves the issue by assigning an initial value to mctr_inhbt.

SBI_HART_HAS_MCOUNTINHIBIT has been tested before accessing mctr_inhbt,

Hi Xiang,

It's true. It's not a bug (as I said, it's a compiler complaint).
But the compiler (gcc) can not know (and ensure) that the used variable is always initialized.

The reported issue will lead to a compiling error now in my environment because of 'Werror=maybe-uninitialized'.

Regards,
Dong

Quoted 25 lines

so there is no need to initialize

Regards,
Xiang W

Quoted 21 lines

---
?lib/sbi/sbi_pmu.c | 2 +-
?1 file changed, 1 insertion(+), 1 deletion(-)

lib/sbi/sbi_pmu.c +0 -0
index a0eb359..5950a20 100644
@@ -489,7 +489,7 @@ static int pmu_ctr_find_hw(unsigned long cbase,

unsigned long cmask, unsigned lo
????????unsigned long ctr_mask;
????????int i, ret = 0, fixed_ctr, ctr_idx = SBI_ENOTSUPP; ????????struct sbi_pmu_hw_event *temp;
-???????unsigned long mctr_inhbt;
+???????unsigned long mctr_inhbt = 0;
????????u32 hartid = current_hartid();
????????struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); ?
--
2.33.1

#3 lore raw

PATCH 1/1 lib: sbi: Resolve the uninitialized complaint in sbi_pmu

In reply to Dong Du

? 2021-11-25???? 18:38 +0800?Dong Du???

Quoted 11 lines

A recent commit (b28f0700) turns to read from CSR_MCOUNTINHIBIT after checking the SBI_HART_HAS_MCOUNTINHIBIT HART feature, which leaves the
mctr_inhbt uninitialized in pmu_ctr_find_hw().
As a result, compiler will complain:
error: 'mctr_inhbt' may be used uninitialized in this function.

This commit resolves the issue by assigning an initial value to mctr_inhbt.

Quoted 22 lines

---
?lib/sbi/sbi_pmu.c | 2 +-
?1 file changed, 1 insertion(+), 1 deletion(-)

lib/sbi/sbi_pmu.c +0 -0
index a0eb359..5950a20 100644
@@ -489,7 +489,7 @@ static int pmu_ctr_find_hw(unsigned long cbase,

unsigned long cmask, unsigned lo
????????unsigned long ctr_mask;
????????int i, ret = 0, fixed_ctr, ctr_idx = SBI_ENOTSUPP; ????????struct sbi_pmu_hw_event *temp;
-???????unsigned long mctr_inhbt;
+???????unsigned long mctr_inhbt = 0;
????????u32 hartid = current_hartid();
????????struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); ?

Signature · 1 line
2.33.1
Applied
#4 lore raw

PATCH 1/1 lib: sbi: Resolve the uninitialized complaint in sbi_pmu

In reply to Dong Du
Quoted 10 lines · Dong Du

A recent commit (b28f0700) turns to read from CSR_MCOUNTINHIBIT after checking the SBI_HART_HAS_MCOUNTINHIBIT HART feature, which leaves the mctr_inhbt uninitialized in pmu_ctr_find_hw().
As a result, compiler will complain:
error: 'mctr_inhbt' may be used uninitialized in this function.

This commit resolves the issue by assigning an initial value to mctr_inhbt.

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 25 lines
---
 lib/sbi/sbi_pmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +1 -2
index a0eb359..5950a20 100644
@@ -489,7 +489,7 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo
        unsigned long ctr_mask;
        int i, ret = 0, fixed_ctr, ctr_idx = SBI_ENOTSUPP;
        struct sbi_pmu_hw_event *temp;
-       unsigned long mctr_inhbt;
+       unsigned long mctr_inhbt = 0;
        u32 hartid = current_hartid();
        struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
 
--

2.33.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi