mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/3] net: dsa: ksz9477: report 0 as value when returning error
Date: Thu,  2 May 2024 17:17:56 +0200	[thread overview]
Message-ID: <20240502151757.3964461-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240502151757.3964461-1-a.fatoum@pengutronix.de>

Most register accesses via the ksz_read* functions don't expect that
they can fail and don't check for an error. This assumption is wrong if
there is a problem with the underlying transport, e.g. an I2C bus.

In that case, the functions would return an error code that mostly goes
unchecked and *val is set to an uninitialized value.

The original Linux driver suffers from a similar problem: There, *val is
only set on success, but if the read fails, there's a warning printed
to log, but still the uninitialized variable in the caller function will
be used instead.

This likely goes unnoticed, because GCC builds on Linux have
-Wno-maybe-uninitialized, but we have implicit -W-maybe-uninitialized in
barebox.

As we sync the code from Linux, one should probably fix this there
first, but let's improve the situation a bit by not reporting an
uninitialized value, but a deterministic zero.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/net/ksz_common.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ksz_common.h b/drivers/net/ksz_common.h
index 291488fe3485..44b5055ee397 100644
--- a/drivers/net/ksz_common.h
+++ b/drivers/net/ksz_common.h
@@ -20,7 +20,7 @@ struct ksz_switch {
 
 static inline int ksz_read8(struct ksz_switch *priv, u32 reg, u8 *val)
 {
-	unsigned int value;
+	unsigned int value = 0;
 	int ret = regmap_read(priv->regmap[0], reg, &value);
 
 	*val = value;
@@ -29,7 +29,7 @@ static inline int ksz_read8(struct ksz_switch *priv, u32 reg, u8 *val)
 
 static inline int ksz_read16(struct ksz_switch *priv, u32 reg, u16 *val)
 {
-	unsigned int value;
+	unsigned int value = 0;
 	int ret = regmap_read(priv->regmap[1], reg, &value);
 
 	*val = value;
@@ -38,7 +38,7 @@ static inline int ksz_read16(struct ksz_switch *priv, u32 reg, u16 *val)
 
 static inline int ksz_read32(struct ksz_switch *priv, u32 reg, u32 *val)
 {
-	unsigned int value;
+	unsigned int value = 0;
 	int ret = regmap_read(priv->regmap[2], reg, &value);
 
 	*val = value;
-- 
2.39.2




  reply	other threads:[~2024-05-02 15:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-02 15:17 [PATCH 1/3] net: dsa: ksz9477: always toggle reset gpio if available Ahmad Fatoum
2024-05-02 15:17 ` Ahmad Fatoum [this message]
2024-05-02 15:17 ` [PATCH 3/3] net: dsa: ksz9477: return negative error codes on PHY access failures Ahmad Fatoum
2024-05-02 15:19 ` [PATCH 1/3] net: dsa: ksz9477: always toggle reset gpio if available Ahmad Fatoum
2024-05-03  6:50 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240502151757.3964461-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox