Skip to contentSkip to conversation

lib: sbi: fix ctz bug

PATCH New Xiang W 3 participants 4 messages Sep 15, 2021 – Sep 22, 2021 RISC-V

lore
Share & tools
lore

Xiang W

author
#0 lore raw

PATCH 1/1 lib: sbi: fix ctz bug

The original version of ctz will cause an endless loop, if the parameter passed in is 0. This commit fixes this bug.

---
 lib/sbi/riscv_asm.c | 3 +++
 1 file changed, 3 insertions(+)
lib/sbi/riscv_asm.c +3 -0
index 4c24a51..d7b9b2b 100644
@@ -213,6 +213,9 @@ static unsigned long ctz(unsigned long x)
 {
 	unsigned long ret = 0;
 
+	if (x == 0)
+		return 8 * sizeof(x);
+
 	while (!(x & 1UL)) {
 		ret++;
 		x = x >> 1;
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/1 lib: sbi: fix ctz bug

Quoted 14 lines · Xiang W

The original version of ctz will cause an endless loop, if the parameter passed in is 0. This commit fixes this bug.

---
 lib/sbi/riscv_asm.c | 3 +++
 1 file changed, 3 insertions(+)
lib/sbi/riscv_asm.c +0 -0
index 4c24a51..d7b9b2b 100644

lib/sbi/riscv_asm.c static unsigned long ctz(unsigned long x) · line 221

Quoted 9 lines
lib/sbi/riscv_asm.c +3 -0
@@ -213,6 +213,9 @@ static unsigned long ctz(unsigned long x)
 {
        unsigned long ret = 0;
 
+       if (x == 0)
+               return 8 * sizeof(x);
+
        while (!(x & 1UL)) {
                ret++;
                x = x >> 1;
#2 lore raw

PATCH 1/1 lib: sbi: fix ctz bug

Quoted 19 lines · Bin Meng
Quoted 17 lines · Xiang W

The original version of ctz will cause an endless loop, if the parameter passed in is 0. This commit fixes this bug.

---
 lib/sbi/riscv_asm.c | 3 +++
 1 file changed, 3 insertions(+)
lib/sbi/riscv_asm.c +0 -0
index 4c24a51..d7b9b2b 100644
@@ -213,6 +213,9 @@ static unsigned long ctz(unsigned long x)
 {
        unsigned long ret = 0;
 
Quoted 8 lines

lib/sbi/riscv_asm.c static unsigned long ctz(unsigned long x) · line 221

Quoted 6 lines
lib/sbi/riscv_asm.c +3 -0
+       if (x == 0)
+               return 8 * sizeof(x);
+
        while (!(x & 1UL)) {
                ret++;
                x = x >> 1;

Looks good to me.

Regards,
Anup

Quoted 5 lines

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

Applied
#3 lore raw

PATCH 1/1 lib: sbi: fix ctz bug

Quoted 30 lines · Anup Patel
Quoted 19 lines · Bin Meng
Quoted 17 lines · Xiang W

The original version of ctz will cause an endless loop, if the parameter passed in is 0. This commit fixes this bug.

---
 lib/sbi/riscv_asm.c | 3 +++
 1 file changed, 3 insertions(+)
lib/sbi/riscv_asm.c +0 -0
index 4c24a51..d7b9b2b 100644
@@ -213,6 +213,9 @@ static unsigned long ctz(unsigned long x)
 {
        unsigned long ret = 0;
 
Quoted 8 lines

lib/sbi/riscv_asm.c static unsigned long ctz(unsigned long x) · line 221

Quoted 6 lines
lib/sbi/riscv_asm.c +3 -0
+       if (x == 0)
+               return 8 * sizeof(x);
+
        while (!(x & 1UL)) {
                ret++;
                x = x >> 1;
Quoted 3 lines

Looks good to me.

Applied this to the riscv/opensbi repo as well.

Regards,
Anup

Quoted 9 lines

Regards,
Anup

Quoted 5 lines

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