public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Andrew F. Davis <afd@ti.com>
To: u-boot@lists.denx.de
Subject: [U-Boot]  [PATCH] timer: Fix possible underflow in udelay
Date: Thu, 25 Aug 2016 13:40:58 -0500	[thread overview]
Message-ID: <20160825184058.2071-1-afd@ti.com> (raw)

When the inputed usec is too large we process it in chunks of
CONFIG_WD_PERIOD size. Subtracting this from usec until usec is
zero. If usec is not an integer multiple of CONFIG_WD_PERIOD it
will underflow and the condition will not become false when it
should. Fix this logic.

Signed-off-by: Andrew F. Davis <afd@ti.com>
---
 lib/time.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/time.c b/lib/time.c
index f37150f..4ec3cb9 100644
--- a/lib/time.c
+++ b/lib/time.c
@@ -145,14 +145,14 @@ void __weak __udelay(unsigned long usec)
 
 void udelay(unsigned long usec)
 {
-	ulong kv;
+	ulong kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
+	ulong elapsed = 0;
 
 	do {
 		WATCHDOG_RESET();
-		kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
-		__udelay (kv);
-		usec -= kv;
-	} while(usec);
+		__udelay(kv);
+		elapsed += kv;
+	} while (elapsed < usec);
 }
 
 void mdelay(unsigned long msec)
-- 
2.9.3

             reply	other threads:[~2016-08-25 18:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-25 18:40 Andrew F. Davis [this message]
2016-08-25 19:02 ` [U-Boot] [PATCH] timer: Fix possible underflow in udelay Troy Kisky
2016-08-25 19:18   ` Andrew F. Davis

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=20160825184058.2071-1-afd@ti.com \
    --to=afd@ti.com \
    --cc=u-boot@lists.denx.de \
    /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