hw/net/rocker: Avoid undefined shifts with more than 31 ports
PATCH 1/1 hw/net/rocker: Avoid undefined shifts with more than 31 ports
In rocker_port_phys_link_status() and rocker_port_phys_enable_read() we construct a 64-bit value with one bit per front-panel port. However we accidentally do the shift as 32-bit arithmetic, which means that if there are more than 31 front-panel ports this is undefined behaviour.
Fix the problem by ensuring we use 64-bit arithmetic for the whole calculation. (We won't ever shift off the 64-bit value because ROCKER_FP_PORTS_MAX is 62.)
Resolves: Coverity CID 1487121, 1487160
--- hw/net/rocker/rocker.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/net/rocker/rocker.c +2 -2
@@ -1010,7 +1010,7 @@ static uint64_t rocker_port_phys_link_status(Rocker *r)FpPort *port = r->fp_port[i];if (fp_port_get_link_up (port)) { - status |=1 << (i +1 ); + status |=1ULL << (i +1 ); } }return status;
@@ -1025,7 +1025,7 @@ static uint64_t rocker_port_phys_enable_read(Rocker *r)FpPort *port = r->fp_port[i];if (fp_port_enabled (port)) { - ret |=1 << (i +1 ); + ret |=1ULL << (i +1 ); } }return ret;
Signature · 1 line
2.25.1
Ping?
thanks
-- PMM
Quoted 39 lines · Peter Maydell
In
rocker_port_phys_link_status()androcker_port_phys_enable_read()we construct a 64-bit value with one bit per front-panel port. However we accidentally do the shift as 32-bit arithmetic, which means that if there are more than 31 front-panel ports this is undefined behaviour.Fix the problem by ensuring we use 64-bit arithmetic for the whole calculation. (We won't ever shift off the 64-bit value because
ROCKER_FP_PORTS_MAXis 62.)Resolves: Coverity CID 1487121, 1487160
Signed-off-by Peter Maydell <[email protected]>--- hw/net/rocker/rocker.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/net/rocker/rocker.c +2
-2@@ -1010,7 +1010,7 @@ static uint64_t rocker_port_phys_link_status(Rocker *r)FpPort *port = r->fp_port[i];if (fp_port_get_link_up (port)) { - status |=1 << (i +1 ); + status |=1ULL << (i +1 ); } }return status;@@ -1025,7 +1025,7 @@ static uint64_t rocker_port_phys_enable_read(Rocker *r)FpPort *port = r->fp_port[i];if (fp_port_enabled (port)) { - ret |=1 << (i +1 ); + ret |=1ULL << (i +1 ); } }return ret;
Richard Henderson
Reviewed-byQuoted 19 lines · Peter Maydell
Ping?
thanks
-- PMMQuoted 13 lines · Peter Maydell
In
rocker_port_phys_link_status()androcker_port_phys_enable_read()we construct a 64-bit value with one bit per front-panel port. However we accidentally do the shift as 32-bit arithmetic, which means that if there are more than 31 front-panel ports this is undefined behaviour.Fix the problem by ensuring we use 64-bit arithmetic for the whole calculation. (We won't ever shift off the 64-bit value because
ROCKER_FP_PORTS_MAXis 62.)Resolves: Coverity CID 1487121, 1487160
Signed-off-by Peter Maydell <[email protected]>
r~
Quoted 27 lines
Quoted 26 lines
--- hw/net/rocker/rocker.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/net/rocker/rocker.c +2
-2@@ -1010,7 +1010,7 @@ static uint64_t rocker_port_phys_link_status(Rocker *r)FpPort *port = r->fp_port[i];if (fp_port_get_link_up (port)) { - status |=1 << (i +1 ); + status |=1ULL << (i +1 ); } }return status;@@ -1025,7 +1025,7 @@ static uint64_t rocker_port_phys_enable_read(Rocker *r)FpPort *port = r->fp_port[i];if (fp_port_enabled (port)) { - ret |=1 << (i +1 ); + ret |=1ULL << (i +1 ); } }return ret;
Quoted 15 lines · Richard Henderson
Quoted 13 lines · Peter Maydell
Ping?
thanks
-- PMMQuoted 7 lines · Peter Maydell
In
rocker_port_phys_link_status()androcker_port_phys_enable_read()we construct a 64-bit value with one bit per front-panel port. However we accidentally do the shift as 32-bit arithmetic, which means that if there are more than 31 front-panel ports this is undefined behaviour.
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Fix the problem by ensuring we use 64-bit arithmetic for the whole calculation. (We won't ever shift off the 64-bit value because
ROCKER_FP_PORTS_MAXis 62.)Resolves: Coverity CID 1487121, 1487160
Signed-off-by Peter Maydell <[email protected]>Reviewed-by Richard Henderson <[email protected]>
Queued.
Thanks
Quoted 32 lines
r~
Quoted 27 lines
Quoted 26 lines
--- hw/net/rocker/rocker.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/net/rocker/rocker.c +2
-2@@ -1010,7 +1010,7 @@ static uint64_t rocker_port_phys_link_status(Rocker *r)FpPort *port = r->fp_port[i];if (fp_port_get_link_up (port)) { - status |=1 << (i +1 ); + status |=1ULL << (i +1 ); } }return status;@@ -1025,7 +1025,7 @@ static uint64_t rocker_port_phys_enable_read(Rocker *r)FpPort *port = r->fp_port[i];if (fp_port_enabled (port)) { - ret |=1 << (i +1 ); + ret |=1ULL << (i +1 ); } }return ret;