Re: [PATCH] net: sfp: remove redundant NULL check

From: Russell King (Oracle)
Date: Thu Feb 15 2024 - 11:04:01 EST


On Sun, Feb 11, 2024 at 07:08:24AM -0800, Daniil Dulov wrote:
> bus->upstream_ops in sfp_register_bus() cannot be NULL. So remove
> redundant NULL check.
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.

It probably would've been better to include in here details of the two
paths that lead to this point, and indicate why it's safe to remove the
NULL check.

The first path is via sfp_register_socket(), which checks that
bus->upstream_ops is not NULL prior to calling sfp_register_bus().
Therefore, "ops" can not be NULL when sfp_register_bus() is called
via this path.

The second path is via sfp_bus_add_upstream(), and this path assumes
that the "ops" passed into this function will not be NULL. Nothing in
this code makes that guarantee, and it's up to the design(er) to
determine whether NULL is permitted or not. It's not something that
an automated checker ought to be suggesting.

In this particular instance, I, as the interface designer, do indeed
intend that "ops" will not be NULL here, so the patch can remove the
check is acceptable in this instance.

However, I'll go back to my original point: this is *not* something
that automated tools should be identifying, and it is *not* something
that should be used to throw patches randomly out, especially where
the commit message doesn't include human analysis details.


>
> Fixes: ce0aa27ff3f6 ("sfp: add sfp-bus to bridge between network devices and sfp cages")
> Signed-off-by: Daniil Dulov <d.dulov@xxxxxxxxxx>
> ---
> drivers/net/phy/sfp-bus.c | 14 ++++++--------
> 1 file changed, 6 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/net/phy/sfp-bus.c b/drivers/net/phy/sfp-bus.c
> index 850915a37f4c..829cb1dccc27 100644
> --- a/drivers/net/phy/sfp-bus.c
> +++ b/drivers/net/phy/sfp-bus.c
> @@ -478,14 +478,12 @@ static int sfp_register_bus(struct sfp_bus *bus)
> const struct sfp_upstream_ops *ops = bus->upstream_ops;
> int ret;
>
> - if (ops) {
> - if (ops->link_down)
> - ops->link_down(bus->upstream);
> - if (ops->connect_phy && bus->phydev) {
> - ret = ops->connect_phy(bus->upstream, bus->phydev);
> - if (ret)
> - return ret;
> - }
> + if (ops->link_down)
> + ops->link_down(bus->upstream);
> + if (ops->connect_phy && bus->phydev) {
> + ret = ops->connect_phy(bus->upstream, bus->phydev);
> + if (ret)
> + return ret;
> }
> bus->registered = true;
> bus->socket_ops->attach(bus->sfp);
> --
> 2.25.1
>
>

--
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!