Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751958AbdLYLLl (ORCPT ); Mon, 25 Dec 2017 06:11:41 -0500 Received: from mail-pg0-f66.google.com ([74.125.83.66]:45805 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750811AbdLYLLi (ORCPT ); Mon, 25 Dec 2017 06:11:38 -0500 X-Google-Smtp-Source: ACJfBou0113IxZrwbE79nYv3Bex6uQF/x5hrDpMv/kxuNcUZYESxo7xDwXqg3yjqUvYbZl1Zz6LUog== From: Baolin Wang To: a.zummo@towertech.it, alexandre.belloni@free-electrons.com Cc: arnd@arndb.de, broonie@kernel.org, linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org, baolin.wang@linaro.org Subject: [PATCH] rtc: Fix overflow when converting time64_t to rtc_time Date: Mon, 25 Dec 2017 19:10:37 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1047 Lines: 32 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 --- 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