pci *_by_mask() coverity fix
cover pci *_by_mask() coverity fix
This patchset fixes a Coverity nit relating to the pci_set_*_by_mask() helper functions, where we might shift off the end of a variable if the caller passes in a bogus mask argument. Patch 2 is the coverity fix (adding an assert() to help Coverity out a bit and to catch potential future actual bugs). Patch 1 removes the _get_ versions of the functions, because they've been in the tree for a decade and never had any callers at any point in those 10 years :-)
thanks
-- PMM
Peter Maydell (2):
pci: Remove unused pci_get_*_by_mask() functions
pci: Sanity check mask argument to pci_set_*_by_mask()
include/hw/pci/pci.h | 48 +++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 32 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/2 pci: Remove unused pci_get_*_by_mask() functions
The helper functions pci_get_{byte,word,long,quad}_by_mask() were added in 2012 in commit c9f50cea70a1596. In the decade since we have never added a single use of them.
The helpers clearly aren't that helpful, so drop them rather than carrying around dead code.
--- include/hw/pci/pci.h | 28 ---------------------------- 1 file changed, 28 deletions(-)
include/hw/pci/pci.h +0 -28
@@ -692,13 +692,6 @@ pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg)pci_set_byte (config, (~mask & val) | (mask & rval)); } -static inline uint8_t -pci_get_byte_by_mask (uint8_t *config,uint8_t mask) -{ -uint8_t val =pci_get_byte (config); -return (val & mask) >>ctz32 (mask); -} -static inline void pci_set_word_by_mask (uint8_t *config,uint16_t mask,uint16_t reg) {
@@ -707,13 +700,6 @@ pci_set_word_by_mask(uint8_t *config, uint16_t mask, uint16_t reg)pci_set_word (config, (~mask & val) | (mask & rval)); } -static inline uint16_t -pci_get_word_by_mask (uint8_t *config,uint16_t mask) -{ -uint16_t val =pci_get_word (config); -return (val & mask) >>ctz32 (mask); -} -static inline void pci_set_long_by_mask (uint8_t *config,uint32_t mask,uint32_t reg) {
@@ -722,13 +708,6 @@ pci_set_long_by_mask(uint8_t *config, uint32_t mask, uint32_t reg)pci_set_long (config, (~mask & val) | (mask & rval)); } -static inline uint32_t -pci_get_long_by_mask (uint8_t *config,uint32_t mask) -{ -uint32_t val =pci_get_long (config); -return (val & mask) >>ctz32 (mask); -} -static inline void pci_set_quad_by_mask (uint8_t *config,uint64_t mask,uint64_t reg) {
@@ -737,13 +716,6 @@ pci_set_quad_by_mask(uint8_t *config, uint64_t mask, uint64_t reg)pci_set_quad (config, (~mask & val) | (mask & rval)); } -static inline uint64_t -pci_get_quad_by_mask (uint8_t *config,uint64_t mask) -{ -uint64_t val =pci_get_quad (config); -return (val & mask) >>ctz32 (mask); -} -PCIDevice *pci_new_multifunction (int devfn,bool multifunction,const char *name);PCIDevice *pci_new (int devfn,const char *name);
Signature · 1 line
2.25.1
Re: [PATCH 1/2] pci: Remove unused pci_get_*_by_mask() functions
Quoted 11 lines · Peter Maydell
The helper functions pci_get_{byte,word,long,quad}_by_mask() were added in 2012 in commit
c9f50cea70a1596. In the decade since we have never added a single use of them.The helpers clearly aren't that helpful, so drop them rather than carrying around dead code.
Signed-off-by Peter Maydell<[email protected]>--- include/hw/pci/pci.h | 28 ---------------------------- 1 file changed, 28 deletions(-)
r~
PATCH 2/2 pci: Sanity check mask argument to pci_set_*_by_mask()
In reply to Peter MaydellCoverity complains that in functions like pci_set_word_by_mask() we might end up shifting by more than 31 bits. This is true, but only if the caller passes in a zero mask. Help Coverity out by asserting that the mask argument is valid.
---
Note that only 1 of these 4 functions is used, and that only in 2 places in the codebase. In both cases the mask argument is a compile-time constant.
--- include/hw/pci/pci.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +16 -4
@@ -688,7 +688,10 @@ static inline void pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg) {uint8_t val =pci_get_byte (config); -uint8_t rval = reg <<ctz32 (mask); +uint8_t rval; + +assert (mask &0xff ); + rval = reg <<ctz32 (mask);pci_set_byte (config, (~mask & val) | (mask & rval)); }
@@ -696,7 +699,10 @@ static inline void pci_set_word_by_mask(uint8_t *config, uint16_t mask, uint16_t reg) {uint16_t val =pci_get_word (config); -uint16_t rval = reg <<ctz32 (mask); +uint16_t rval; + +assert (mask &0xffff ); + rval = reg <<ctz32 (mask);pci_set_word (config, (~mask & val) | (mask & rval)); }
@@ -704,7 +710,10 @@ static inline void pci_set_long_by_mask(uint8_t *config, uint32_t mask, uint32_t reg) {uint32_t val =pci_get_long (config); -uint32_t rval = reg <<ctz32 (mask); +uint32_t rval; + +assert (mask); + rval = reg <<ctz32 (mask);pci_set_long (config, (~mask & val) | (mask & rval)); }
@@ -712,7 +721,10 @@ static inline void pci_set_quad_by_mask(uint8_t *config, uint64_t mask, uint64_t reg) {uint64_t val =pci_get_quad (config); -uint64_t rval = reg <<ctz32 (mask); +uint64_t rval; + +assert (mask); + rval = reg <<ctz32 (mask);pci_set_quad (config, (~mask & val) | (mask & rval)); }
Signature · 1 line
2.25.1
Re: [PATCH 2/2] pci: Sanity check mask argument to pci_set_*_by_mask()
Quoted 7 lines · Peter Maydell
Coverity complains that in functions like
pci_set_word_by_mask()we might end up shifting by more than 31 bits. This is true, but only if the caller passes in a zero mask. Help Coverity out by asserting that the mask argument is valid.Fixes CID 1487168
Quoted 8 lines
Signed-off-by Peter Maydell <[email protected]>---
Note that only 1 of these 4 functions is used, and that only in 2 places in the codebase. In both cases the mask argument is a compile-time constant.--- include/hw/pci/pci.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
Re: [PATCH 2/2] pci: Sanity check mask argument to pci_set_*_by_mask()
In reply to Peter MaydellQuoted 21 lines · Peter Maydell
Coverity complains that in functions like
pci_set_word_by_mask()we might end up shifting by more than 31 bits. This is true, but only if the caller passes in a zero mask. Help Coverity out by asserting that the mask argument is valid.Fixes CID 1487168Signed-off-by Peter Maydell <[email protected]>---
Note that only 1 of these 4 functions is used, and that only in 2 places in the codebase. In both cases the mask argument is a compile-time constant.--- include/hw/pci/pci.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)include/hw/pci/pci.h +0
-0
include/hw/pci/pci.h static inline void · line 693
Quoted 7 lines
include/hw/pci/pci.h +3
-1@@ -688,7 +688,10 @@ static inline void pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg) {uint8_t val =pci_get_byte (config); -uint8_t rval = reg <<ctz32 (mask); +uint8_t rval; + +assert (mask &0xff );
Why the and, especially considering the uint8_t type?
include/hw/pci/pci.h static inline void · line 704
Quoted 8 lines
include/hw/pci/pci.h +3
-1@@ -696,7 +699,10 @@ static inline void pci_set_word_by_mask(uint8_t *config, uint16_t mask, uint16_t reg) {uint16_t val =pci_get_word (config); -uint16_t rval = reg <<ctz32 (mask); +uint16_t rval; + +assert (mask &0xffff );
Similarly.
Otherwise,
r~
Re: [PATCH 2/2] pci: Sanity check mask argument to pci_set_*_by_mask()
Quoted 18 lines · Richard Henderson
Quoted 16 lines · Peter Maydell
Coverity complains that in functions like
pci_set_word_by_mask()we might end up shifting by more than 31 bits. This is true, but only if the caller passes in a zero mask. Help Coverity out by asserting that the mask argument is valid.Fixes CID 1487168Signed-off-by Peter Maydell <[email protected]>---
Note that only 1 of these 4 functions is used, and that only in 2 places in the codebase. In both cases the mask argument is a compile-time constant.--- include/hw/pci/pci.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
Quoted 14 lines
include/hw/pci/pci.h static inline void · line 693
Quoted 12 lines
include/hw/pci/pci.h +3
-1@@ -688,7 +688,10 @@ static inline void pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg) {uint8_t val =pci_get_byte (config); -uint8_t rval = reg <<ctz32 (mask); +uint8_t rval; + +assert (mask &0xff );Why the and, especially considering the
uint8_ttype?
Oops, yes. I think I was mixing up prototypes and thought the mask was passed as a 32-bit value in both these functions.
-- PMM
Re: [PATCH 2/2] pci: Sanity check mask argument to pci_set_*_by_mask()
Quoted 35 lines · Peter Maydell
Quoted 18 lines · Richard Henderson
Quoted 16 lines · Peter Maydell
Coverity complains that in functions like
pci_set_word_by_mask()we might end up shifting by more than 31 bits. This is true, but only if the caller passes in a zero mask. Help Coverity out by asserting that the mask argument is valid.Fixes CID 1487168Signed-off-by Peter Maydell <[email protected]>---
Note that only 1 of these 4 functions is used, and that only in 2 places in the codebase. In both cases the mask argument is a compile-time constant.--- include/hw/pci/pci.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)Quoted 14 lines
include/hw/pci/pci.h static inline void · line 693
Quoted 12 lines
include/hw/pci/pci.h +3
-1@@ -688,7 +688,10 @@ static inline void pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg) {uint8_t val =pci_get_byte (config); -uint8_t rval = reg <<ctz32 (mask); +uint8_t rval; + +assert (mask &0xff );Why the and, especially considering the
uint8_ttype?
Quoted 4 lines
Oops, yes. I think I was mixing up prototypes and thought the mask was passed as a 32-bit value in both these functions.
-- PMM
Did you intend to send v2 of this without the &?
Signature · 1 line
MST
Re: [PATCH 2/2] pci: Sanity check mask argument to pci_set_*_by_mask()
Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Peter Maydell
Quoted 32 lines · Richard Henderson
Quoted 21 lines · Peter Maydell
Coverity complains that in functions like
pci_set_word_by_mask()we might end up shifting by more than 31 bits. This is true, but only if the caller passes in a zero mask. Help Coverity out by asserting that the mask argument is valid.Fixes CID 1487168Signed-off-by Peter Maydell <[email protected]>---
Note that only 1 of these 4 functions is used, and that only in 2 places in the codebase. In both cases the mask argument is a compile-time constant.--- include/hw/pci/pci.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)include/hw/pci/pci.h +0
-0include/hw/pci/pci.h static inline void · line 693
Quoted 7 lines
include/hw/pci/pci.h +3
-1@@ -688,7 +688,10 @@ static inline void pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg) {uint8_t val =pci_get_byte (config); -uint8_t rval = reg <<ctz32 (mask); +uint8_t rval; + +assert (mask &0xff );Why the and, especially considering the
uint8_ttype?
Quoted 4 lines
Quoted 2 lines
Oops, yes. I think I was mixing up prototypes and thought the mask was passed as a 32-bit value in both these functions.
Did you intend to send v2 of this without the &?
Thanks for the reminder -- I'd forgotten I needed to respin.
-- PMM