[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <ZIxtfFTYaX2yxlFO@shell.armlinux.org.uk>
Date: Fri, 16 Jun 2023 15:11:08 +0100
From: "Russell King (Oracle)" <linux@...linux.org.uk>
To: Jianhui Zhao <zhaojh329@...il.com>
Cc: andrew@...n.ch, davem@...emloft.net, edumazet@...gle.com,
hkallweit1@...il.com, kuba@...nel.org,
linux-kernel@...r.kernel.org, netdev@...r.kernel.org,
pabeni@...hat.com
Subject: Re: [PATCH V4] net: phy: Add sysfs attribute for PHY c45 identifiers.
On Fri, Jun 16, 2023 at 09:54:55PM +0800, Jianhui Zhao wrote:
> >+ struct phy_c45_devid_attribute *devattr =
> >+ container_of(attr, struct phy_c45_devid_attribute, attr);
>
> >+ struct phy_c45_devid_attribute *devattr =
> >+ (struct phy_c45_devid_attribute *)container_of(attr, struct device_attribute, attr);
>
> The two conversions is not same.
You're right...
> One is convert "struct device_attribute" to "struct phy_c45_devid_attribute",
> and another one is convert "struct attribute" to "struct phy_c45_devid_attribute".
> The second one must cast "struct device_attribute" returned from container_of
> to "struct phy_c45_devid_attribute".
... but this isn't entirely correct.
Doing it properly:
struct phy_c45_devid_attribute *devattr =
container_of(attr, struct phy_c45_devid_attribute, attr.attr);
Number one rule with C programming: if you have to cast, you're
probably doing something wrong, so always look to see if there's an
alternative solution that doesn't involve casts.
Casts are one of the reasons why programming errors happen - it stops
the compiler being able to perform proper type checking. Always avoid
them as much as possible.
--
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Powered by blists - more mailing lists