gpio: pca953x: avoid to use uninitialized value pinctrl
PATCH v2 1/4 gpio: pca953x: avoid to use uninitialized value pinctrl
From: Haibo Chen <[email protected]>
There is a variable pinctrl declared without initializer. And then has the case (switch operation chose the default case) to directly use this uninitialized value, this is not a safe behavior. So here initialize the pinctrl as 0 to avoid this issue.
This is reported by Coverity.
---
v2: used default case (Andy)
drivers/gpio/gpio-pca953x.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/gpio/gpio-pca953x.c +3 -0
@@ -474,6 +474,9 @@ static u8 pcal6534_recalc_addr(struct pca953x_chip *chip, int reg, int off)case PCAL6524_DEBOUNCE : pinctrl = ((reg &PCAL_PINCTRL_MASK ) >>1 ) +0x1c ;break ; +default : + pinctrl =0 ; +break ; }return pinctrl + addr + (off /BANK_SZ );
Signature · 1 line
2.35.1
PATCH v2 2/4 gpio: pca953x: avoid logically dead code
From: Haibo Chen <[email protected]>
The current code logic make the condition "else if (reg >= 0x54)" can't be true, cause the dead code. So fix it to match the coder expectation. This is reported by Coverity.
---
v2: shuffled code to check bigger value first (Andy)
drivers/gpio/gpio-pca953x.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/gpio/gpio-pca953x.c +10 -10
@@ -309,26 +309,26 @@ static bool pcal6534_check_register(struct pca953x_chip *chip, unsigned int reg,int bank;int offset; -if (reg >=0x30 ) { +if (reg >=0x54 ) {/* -* Reserved block between 14h and 2Fh does not align on -* expected bank boundaries like other devices. +* Handle lack of reserved registers after output port +* configuration register to form a bank. */ -int temp = reg -0x30 ; +int temp = reg -0x54 ; bank = temp /NBANK (chip); offset = temp - (bank *NBANK (chip)); - bank +=8 ; - }else if (reg >=0x54 ) { + bank +=16 ; + }else if (reg >=0x30 ) {/* -* Handle lack of reserved registers after output port -* configuration register to form a bank. +* Reserved block between 14h and 2Fh does not align on +* expected bank boundaries like other devices. */ -int temp = reg -0x54 ; +int temp = reg -0x30 ; bank = temp /NBANK (chip); offset = temp - (bank *NBANK (chip)); - bank +=16 ; + bank +=8 ; }else { bank = reg /NBANK (chip); offset = reg - (bank *NBANK (chip));
Signature · 1 line
2.35.1
PATCH v2 3/4 gpio: pca953x: Clean up pcal6534_check_register()
In reply to Andy ShevchenkoThe pcal6534_check_register() is a bit too verbose. Clean up it, by deduplicating some operations and switching to the modulo operation as on some architectures / and % can become a single assembly instruction.
---
v2: new patch
drivers/gpio/gpio-pca953x.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)
drivers/gpio/gpio-pca953x.c +9 -12
@@ -306,6 +306,7 @@ static bool pca953x_check_register(struct pca953x_chip *chip, unsigned int reg,static bool pcal6534_check_register (struct pca953x_chip *chip,unsigned int reg,u32 checkbank) { +int bank_shift;int bank;int offset;
@@ -314,26 +315,22 @@ static bool pcal6534_check_register(struct pca953x_chip *chip, unsigned int reg,* Handle lack of reserved registers after output port * configuration register to form a bank. */ -int temp = reg -0x54 ; - - bank = temp /NBANK (chip); - offset = temp - (bank *NBANK (chip)); - bank +=16 ; + reg -=0x54 ; + bank_shift =16 ; }else if (reg >=0x30 ) {/* * Reserved block between 14h and 2Fh does not align on * expected bank boundaries like other devices. */ -int temp = reg -0x30 ; - - bank = temp /NBANK (chip); - offset = temp - (bank *NBANK (chip)); - bank +=8 ; + reg -=0x30 ; + bank_shift =8 ; }else { - bank = reg /NBANK (chip); - offset = reg - (bank *NBANK (chip)); + bank_shift =0 ; } + bank = bank_shift + reg /NBANK (chip); + offset = reg %NBANK (chip); +/* Register is not in the matching bank. */ if (!(BIT (bank) & checkbank))return false;
Signature · 1 line
2.35.1
PATCH v2 4/4 gpio: pca953x: Remove unused PCAL953X_OUT_CONF from pcal6534_recalc_addr()
In reply to Andy ShevchenkoFirst of all, PCAL953X_OUT_CONF is not used in the driver. Second, it's not a per-bank register, it's a single for the chip and should be handled differently anyway.
To avoid confusion, drop PCAL953X_OUT_CONF from pcal6534_recalc_addr().
---
v2: new patch
drivers/gpio/gpio-pca953x.c | 1 - 1 file changed, 1 deletion(-)
drivers/gpio/gpio-pca953x.c +0 -1
@@ -461,7 +461,6 @@ static u8 pcal6534_recalc_addr(struct pca953x_chip *chip, int reg, int off)case PCAL953X_PULL_SEL :case PCAL953X_INT_MASK :case PCAL953X_INT_STAT : -case PCAL953X_OUT_CONF : pinctrl = ((reg &PCAL_PINCTRL_MASK ) >>1 ) +0x20 ;break ;case PCAL6524_INT_EDGE :
Signature · 1 line
2.35.1
Quoted 7 lines · Andy Shevchenko
From: Haibo Chen <[email protected]>
There is a variable pinctrl declared without initializer. And then has the case (switch operation chose the default case) to directly use this uninitialized value, this is not a safe behavior. So here initialize the pinctrl as 0 to avoid this issue.
This is reported by Coverity.
Bart, any comments on the series?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
From: Haibo Chen <[email protected]>
Quoted 8 lines
Quoted 5 lines
There is a variable pinctrl declared without initializer. And then has the case (switch operation chose the default case) to directly use this uninitialized value, this is not a safe behavior. So here initialize the pinctrl as 0 to avoid this issue.
This is reported by Coverity.Bart, any comments on the series?
Now applied. I just got back from Christmas break, give me a moment. :)
Bart
Quoted 11 lines · Bartosz Golaszewski
Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
From: Haibo Chen <[email protected]>
There is a variable pinctrl declared without initializer. And then has the case (switch operation chose the default case) to directly use this uninitialized value, this is not a safe behavior. So here initialize the pinctrl as 0 to avoid this issue.
This is reported by Coverity.
Quoted 3 lines
Quoted 1 line
Bart, any comments on the series?
Now applied. I just got back from Christmas break, give me a moment. :)
Sure and thank you! Merry xmas!
Signature · 2 lines
With Best Regards, Andy Shevchenko