2018-04-20 09:06:04

by Colin King

[permalink] [raw]
Subject: [PATCH] isci: Fix infinite loop in while loop

From: Colin Ian King <[email protected]>

In the case when the phy_mask is bitwise anded with the
phy_index bit is zero the continue statement currently jumps
to the next iteration of the while loop and phy_index is
never actually incremented, potentially causing an infinite
loop if phy_index is less than SCI_MAX_PHS. Fix this by
jumping to the increment of phy_index.

[ The goto is used to save one more level of nesting that
makes the code far wider than 80 columns. ]

Fixes: 80aebef7c112 ("[SCSI] isci: Fix a infinite loop.")

Signed-off-by: Colin Ian King <[email protected]>
---
drivers/scsi/isci/port_config.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/isci/port_config.c b/drivers/scsi/isci/port_config.c
index edb7be786c65..55dc7c1dbc2b 100644
--- a/drivers/scsi/isci/port_config.c
+++ b/drivers/scsi/isci/port_config.c
@@ -293,7 +293,7 @@ sci_mpc_agent_validate_phy_configuration(struct isci_host *ihost,
* This is expected and required to add the phy to the port. */
while (phy_index < SCI_MAX_PHYS) {
if ((phy_mask & (1 << phy_index)) == 0)
- continue;
+ goto next_index;
sci_phy_get_sas_address(&ihost->phys[phy_index],
&phy_assigned_address);

@@ -311,6 +311,7 @@ sci_mpc_agent_validate_phy_configuration(struct isci_host *ihost,
&ihost->phys[phy_index]);

assigned_phy_mask |= (1 << phy_index);
+next_index:
phy_index++;
}

--
2.17.0



2018-04-20 09:48:39

by James Bottomley

[permalink] [raw]
Subject: Re: [PATCH] isci: Fix infinite loop in while loop

On Fri, 2018-04-20 at 10:03 +0100, Colin King wrote:
> From: Colin Ian King <[email protected]>
>
> In the case when the phy_mask is bitwise anded with the
> phy_index bit is zero the continue statement currently jumps
> to the next iteration of the while loop and phy_index is
> never actually incremented, potentially causing an infinite
> loop if phy_index is less than SCI_MAX_PHS. Fix this by
> jumping to the increment of phy_index.
>
> [ The goto is used to save one more level of nesting that
> makes the code far wider than 80 columns. ]

what's wrong with replacing the while() with a for() that just works
(removing the increment at the end). This is effectively open coding a
for loop anyway, which is a pattern we wouldn't want replicated.

James


2018-04-20 09:58:56

by Colin King

[permalink] [raw]
Subject: Re: [PATCH] isci: Fix infinite loop in while loop

On 20/04/18 10:45, James Bottomley wrote:
> On Fri, 2018-04-20 at 10:03 +0100, Colin King wrote:
>> From: Colin Ian King <[email protected]>
>>
>> In the case when the phy_mask is bitwise anded with the
>> phy_index bit is zero the continue statement currently jumps
>> to the next iteration of the while loop and phy_index is
>> never actually incremented, potentially causing an infinite
>> loop if phy_index is less than SCI_MAX_PHS. Fix this by
>> jumping to the increment of phy_index.
>>
>> [ The goto is used to save one more level of nesting that
>> makes the code far wider than 80 columns. ]
>
> what's wrong with replacing the while() with a for() that just works
> (removing the increment at the end). This is effectively open coding a
> for loop anyway, which is a pattern we wouldn't want replicated.
>
> James
>
Good point, V2 en-route.