All of lore.kernel.org
 help / color / mirror / Atom feed
* [rtc-linux] [PATCH] rtc: fix rtc_time64_to_tm calculation
@ 2016-01-07 12:11 ` Andrzej Hajda
  0 siblings, 0 replies; 4+ messages in thread
From: Andrzej Hajda @ 2016-01-07 12:11 UTC (permalink / raw)
  To: Alessandro Zummo, Alexandre Belloni,
	open list:REAL TIME CLOCK (RTC) SUBSYSTEM
  Cc: Andrzej Hajda, Bartlomiej Zolnierkiewicz, Marek Szyprowski,
	open list

Type of local variable days has been changed recently to unsigned,
but it can take negative values. As a result it works incorrectly for some
arguments. The patch fixes it.

The problem has been detected using proposed semantic patch
scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci [1].

[1]: http://permalink.gmane.org/gmane.linux.kernel/2120705

Fixes: cab572b82c4b ('rtc: fix overflow and incorrect calculation in rtc_time64_to_tm')
Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
---
 drivers/rtc/rtc-lib.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c
index cf2b23c..6323d13 100644
--- a/drivers/rtc/rtc-lib.c
+++ b/drivers/rtc/rtc-lib.c
@@ -54,7 +54,7 @@ void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
 {
 	unsigned int month, year;
 	int secs;
-	unsigned long days;
+	unsigned long days, leaps;
 
 	/*
 	 * time must be positive
@@ -66,13 +66,16 @@ void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
 	tm->tm_wday = (days + 4) % 7;
 
 	year = 1970 + days / 365;
-	days -= (year - 1970) * 365
-		+ LEAPS_THRU_END_OF(year - 1)
-		- LEAPS_THRU_END_OF(1970 - 1);
-	if (days < 0) {
+	days -= (year - 1970) * 365;
+	leaps = LEAPS_THRU_END_OF(year - 1) - LEAPS_THRU_END_OF(1970 - 1);
+
+	while (days < leaps) {
 		year -= 1;
 		days += 365 + is_leap_year(year);
 	}
+
+	days -= leaps;
+
 	tm->tm_year = year - 1900;
 	tm->tm_yday = days + 1;
 
-- 
1.9.1

-- 
-- 
You received this message because you are subscribed to "rtc-linux".
Membership options at http://groups.google.com/group/rtc-linux .
Please read http://groups.google.com/group/rtc-linux/web/checklist
before submitting a driver.
--- 
You received this message because you are subscribed to the Google Groups "rtc-linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rtc-linux+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-01-07 17:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-07 12:11 [rtc-linux] [PATCH] rtc: fix rtc_time64_to_tm calculation Andrzej Hajda
2016-01-07 12:11 ` Andrzej Hajda
2016-01-07 17:24 ` [rtc-linux] " Alexandre Belloni
2016-01-07 17:24   ` Alexandre Belloni

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.