From: Alexandre Belloni <alexandre.belloni@free-electrons.com>
To: Andrzej Hajda <a.hajda@samsung.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>,
"open list:REAL TIME CLOCK (RTC) SUBSYSTEM"
<rtc-linux@googlegroups.com>,
Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>,
Marek Szyprowski <m.szyprowski@samsung.com>,
open list <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] rtc: fix rtc_time64_to_tm calculation
Date: Thu, 7 Jan 2016 18:24:26 +0100 [thread overview]
Message-ID: <20160107172426.GA3988@piout.net> (raw)
In-Reply-To: <1452168710-16317-1-git-send-email-a.hajda@samsung.com>
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
prev parent reply other threads:[~2016-01-07 17:24 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-01-07 12:11 [PATCH] rtc: fix rtc_time64_to_tm calculation Andrzej Hajda
2016-01-07 17:24 ` Alexandre Belloni [this message]
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=20160107172426.GA3988@piout.net \
--to=alexandre.belloni@free-electrons.com \
--cc=a.hajda@samsung.com \
--cc=a.zummo@towertech.it \
--cc=b.zolnierkie@samsung.com \
--cc=linux-kernel@vger.kernel.org \
--cc=m.szyprowski@samsung.com \
--cc=rtc-linux@googlegroups.com \
/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;
as well as URLs for NNTP newsgroup(s).