From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew F. Davis Date: Thu, 25 Aug 2016 13:40:58 -0500 Subject: [U-Boot] [PATCH] timer: Fix possible underflow in udelay Message-ID: <20160825184058.2071-1-afd@ti.com> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de 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 --- 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