All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jean Delvare <khali@linux-fr.org>
To: lm-sensors@vger.kernel.org
Subject: Re: [lm-sensors] [PATCH] hwmon: Add missing parentheses
Date: Wed, 18 Feb 2009 15:15:17 +0000	[thread overview]
Message-ID: <20090218161517.160d6e8d@hyperion.delvare> (raw)
In-Reply-To: <499AB902.1080308@gmail.com>

Hi Hans,

On Wed, 18 Feb 2009 15:25:52 +0100, Hans de Goede wrote:
> Jean Delvare wrote:
> > On Tue, 17 Feb 2009 21:03:23 +0000, Alistair John Strachan wrote:
> >> This one's been there from the beginning.
> >>
> >> The change looks right to me. (i * count) account the previous successes, add 
> >> either zero or x depending on the kind of read failure (timeout/short read vs 
> >> other I/O error). I'm not entirely sure why i must be >0 for accounting the 
> >> short read.
> > 
> > I can't make any sense of that "i &&" either. Hans?
> 
> This is in an error handling path, if some bytes were read but not count bytes, 
> then we return i * count + x, where i * count is the number of bytes read 
> successful so far and x is the number of bytes read for the offset we were 
> reading from when we got the short read.
> 
> However if this is not just a short read, but a read which error-ed out then x 
> < 0. If we then would return i * count + x, we would return less then what 
> we've actually read. So in this case we want to return just i * count, which 
> gets done in the form of i * count + 0.

So far so good...

> There is one special case however if we get an error code in the form of x < 0, 
> and we've not done any successfull reads (so i = 0), then the right thing to 
> do is to pass the error code along. So the condition in which we want to add 0 
> to the result instead of x becomes (i && x < 0)
> 
> Hopes this helps.

OK... I get the idea now. I'm glad you were still around to explain it
because I would never have figured it out. And I don't really like the
logic: behaving differently if the error occurs during the first read
or the later ones doesn't make a lot of sense to me. If the error is so
important that it needs to be reported, then I say it should be
reported later as well. Or seen the other way around, if it doesn't
need to be reported on later reads, then why bother reporting it on the
first read (rather than just returning 0)?

Roel's patch is correct and could be applied. That being said, I don't
like that kind of tricky code that needs to be explained to you. I'd
rather rewrite it in a simpler form which can be understood directly.
Especially given the fact that all the callers care about is boolean
success or failure. So I'd suggest the following code clean-up:

---
 drivers/hwmon/abituguru3.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

--- linux-2.6.29-rc5.orig/drivers/hwmon/abituguru3.c	2009-01-17 09:06:19.000000000 +0100
+++ linux-2.6.29-rc5/drivers/hwmon/abituguru3.c	2009-02-18 16:07:51.000000000 +0100
@@ -760,8 +760,11 @@ static int abituguru3_read_increment_off
 
 	for (i = 0; i < offset_count; i++)
 		if ((x = abituguru3_read(data, bank, offset + i, count,
-				buf + i * count)) != count)
-			return i * count + (i && (x < 0)) ? 0 : x;
+				buf + i * count)) != count) {
+			if (x < 0)
+				return x;
+			return i * count + x;
+		}
 
 	return i * count;
 }

Objections?

-- 
Jean Delvare

_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

  parent reply	other threads:[~2009-02-18 15:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-17 13:17 [lm-sensors] [PATCH] hwmon: Add missing parentheses Roel Kluin
2009-02-17 21:03 ` Alistair John Strachan
2009-02-18 13:17 ` Jean Delvare
2009-02-18 14:25 ` Hans de Goede
2009-02-18 15:15 ` Jean Delvare [this message]
2009-02-18 15:15 ` Hans de Goede
2009-02-19  9:13 ` alistair
2009-02-19  9:28 ` Hans de Goede
2009-02-19  9:41 ` Jean Delvare

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=20090218161517.160d6e8d@hyperion.delvare \
    --to=khali@linux-fr.org \
    --cc=lm-sensors@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.