rtc: Fix overflow when converting time64_t to rtc_time

Message ID a63f2b482281004df854d8345cb18368224880b6.1514200190.git.baolin.wang@linaro.org
State New
Headers show
Series
  • rtc: Fix overflow when converting time64_t to rtc_time
Related show

Commit Message

Baolin Wang Dec. 25, 2017, 11:10 a.m.
If we convert one large time values to rtc_time, in the original formula
'days * 86400' can be overflowed in 'unsigned int' type to make the formula
get one incorrect remain seconds value. Thus we can use div_s64_rem()
function to avoid this situation.

Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
 drivers/rtc/rtc-lib.c |    6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Baolin Wang Jan. 8, 2018, 6 a.m. | #1
Hi Alexandre,

On 25 December 2017 at 19:10, Baolin Wang <baolin.wang@linaro.org> wrote:
> If we convert one large time values to rtc_time, in the original formula
> 'days * 86400' can be overflowed in 'unsigned int' type to make the formula
> get one incorrect remain seconds value. Thus we can use div_s64_rem()
> function to avoid this situation.
>
> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>

Could you apply this patch if there are no other comments? Thanks.

> ---
>  drivers/rtc/rtc-lib.c |    6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c
> index 1ae7da5..ad5bb21 100644
> --- a/drivers/rtc/rtc-lib.c
> +++ b/drivers/rtc/rtc-lib.c
> @@ -52,13 +52,11 @@ int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
>   */
>  void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
>  {
> -       unsigned int month, year;
> -       unsigned long secs;
> +       unsigned int month, year, secs;
>         int days;
>
>         /* time must be positive */
> -       days = div_s64(time, 86400);
> -       secs = time - (unsigned int) days * 86400;
> +       days = div_s64_rem(time, 86400, &secs);
>
>         /* day of the week, 1970-01-01 was a Thursday */
>         tm->tm_wday = (days + 4) % 7;
> --
> 1.7.9.5
>

Patch

diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c
index 1ae7da5..ad5bb21 100644
--- a/drivers/rtc/rtc-lib.c
+++ b/drivers/rtc/rtc-lib.c
@@ -52,13 +52,11 @@  int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
  */
 void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
 {
-	unsigned int month, year;
-	unsigned long secs;
+	unsigned int month, year, secs;
 	int days;
 
 	/* time must be positive */
-	days = div_s64(time, 86400);
-	secs = time - (unsigned int) days * 86400;
+	days = div_s64_rem(time, 86400, &secs);
 
 	/* day of the week, 1970-01-01 was a Thursday */
 	tm->tm_wday = (days + 4) % 7;