Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1303533 > unrolled thread
| Started by | Andrzej Hajda <a.hajda@samsung.com> |
|---|---|
| First post | 2016-01-07 13:20 +0100 |
| Last post | 2016-01-07 18:30 +0100 |
| Articles | 2 — 2 participants |
Back to article view | Back to linux.kernel
[PATCH] rtc: fix rtc_time64_to_tm calculation Andrzej Hajda <a.hajda@samsung.com> - 2016-01-07 13:20 +0100
Re: [PATCH] rtc: fix rtc_time64_to_tm calculation Alexandre Belloni <alexandre.belloni@free-electrons.com> - 2016-01-07 18:30 +0100
| From | Andrzej Hajda <a.hajda@samsung.com> |
|---|---|
| Date | 2016-01-07 13:20 +0100 |
| Subject | [PATCH] rtc: fix rtc_time64_to_tm calculation |
| Message-ID | <qOhHR-7YB-35@gated-at.bofh.it> |
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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
[toc] | [next] | [standalone]
| From | Alexandre Belloni <alexandre.belloni@free-electrons.com> |
|---|---|
| Date | 2016-01-07 18:30 +0100 |
| Message-ID | <qOmxQ-2P2-11@gated-at.bofh.it> |
| In reply to | #1303533 |
On 07/01/2016 at 13:11:50 +0100, Andrzej Hajda wrote :
> 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.
>
Yeah, I was not planning to send that patch finally because it had more
issues than expected. What you did seems good, I'll have a closer look
when I'm back from vacations. My current internet access is too limited
to be able to pull/push anything with git.
> 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
>
--
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web