Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 70B6CC433F5 for ; Sun, 24 Apr 2022 09:35:06 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 79D6983AD1; Sun, 24 Apr 2022 11:35:02 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=web.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=web.de header.i=@web.de header.b="GlVGazon"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E08F683AE2; Sun, 24 Apr 2022 11:34:59 +0200 (CEST) Received: from mout.web.de (mout.web.de [212.227.17.11]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 3C5ED83A9F for ; Sun, 24 Apr 2022 11:34:57 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=web.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jan.kiszka@web.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1650792896; bh=ZKS/qIpLNA7SHcsLmwNcGOo6W7o6xKR5JfePNvRElYs=; h=X-UI-Sender-Class:Date:From:Subject:To; b=GlVGazon7heB0bRkutTiwBbzrXha3ij69sBHkTlBbTA6f+tO4NwjtGVSg2+KJ3Qpo oEe2llDzsYoeA1k7cFnEjZDKpdjLT0bkYjKVmcF1ggYEUVlCGJTbg2deJmOK+8yGqh QHPFBKfilDMd8g3SOMTKkAfvYM+FZpHKtH8JexOY= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.10.10] ([88.215.102.122]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MOlwj-1nWmWl34MJ-00QGeQ for ; Sun, 24 Apr 2022 11:34:56 +0200 Message-ID: <8fff2115-dcfc-a1ea-579a-74d124811869@web.de> Date: Sun, 24 Apr 2022 11:34:56 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 From: Jan Kiszka Subject: [PATCH] lib/date: Make rtc_mktime and mktime64 Y2038-ready Content-Language: en-US To: U-Boot Mailing List Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:+Ir30zBW9wMhj7GqOQVNGxsqksgpzqT9CjFFrob8UWDclahxb+Z +vRH9lJHiYxZFIJ2NJIS2B+KCK7QLyBWVru0vYYiVnz67nJp+ARrGS57wTroynK1vk1BPox EpLpBafsAnZql/RBn7ea0JN5/hraGe9bM6CP+2rJ8nS3Sadnl67vjFjlAXYbWPKxICJVX+0 mmQC4B01MiQcFMVhOkeeQ== X-UI-Out-Filterresults: notjunk:1;V03:K0:bxamnj23eBw=:GrTdlUs1TH4Jee0kxlEBs1 LZdFnL5RDwdtXieM7C57FmSyhc3yMW6gzJSvEidLjbM0M8txQEQhURFw1ZVbJyAkH4rBSuu9Z h22GZEzxSJu7SOuO8e3qboIt94M0NT4nPmUqXomdFHyJUzyoW1NThEDgLfA65ooasL3bmqxi9 wCURSDcLsRU8vWQh+TO5h7GEX5LmH2htj/IJO9eC2D88O8pmDSeMPeByQ7+d2/VKyLG3f3oGa foSzpttR/K58jPIodYEafqLnBksAFza88viPG2Fmdr3tJ6ccOuUKVwQVaZ787ik2BPiWP48/8 LJ43tlWlIbgN926Dx24V9tqvMHBxsUljZv/2/JQzfrxs8gWMDsTZB/5Fhi6KIUn92nAoj9VL6 CPkhv8wqjFKImhZ8qDuOfCP2CX/6QrP65EZTGsuE5cWW1WfNLG3/a5yLLBON/P54VxW93I6et cbnDvJRrOqPN2atrByoXOGtbH8zhFHCniLhQZYuMdMgVF8wfrVNURwcdpLL6ePcO5vx8Vov64 xkGYfkJHc6XktpwLi1TohQMLgK/KCY/hvrysBBFoDPxO6aZ9iV5ijEfukr8XJC0MbHcVRBu0R AkSCW0emW+kmsbbHpTrw3XdgMf1J7fDkiaa7FzthW8AuM0BHSekGA5j9mBSMU9aUYzFjOhOoG NN3mx/y9zlqJhEWXUB3CY1Z+wc/L/XWi+42KOGYWJLH5K0hSdjO+bE+uFBuesvdLr6/fGQF99 Du9Arr9J+rUSKg7UnKJwHuAJXBGWzbrQuc6c0fmrJpmLHjvx9V31+LTJ6iNWduB1T9pvEbiw3 v4Nto+mPV3cUOfhmlcgmjQLo8HfFnBHbzAjLFHVf+1WqiBq+W+6Jli1LzYUDCdc786jspAByR tRSO/w0nuB8f3Rew4fjf8GS5ISNJnIMgb0HKJWBLCHA3U9oEWFPHOlyOT//MTInNohMahWBPh 5ELLhz34eHpnMss7dYR28E2Lmfox765boHA9HiQ62g43QwBGDKnpz6G1KXDvArtV/8gMydqSN lA+tfP74E1fmBcNEeOzjhLZNHAscrlyRk1RHLh2KJGOKCpt51SGbap8lK7kf2jt9TNwgHExpe hRB8DB4EcBXt6w= X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean From: Jan Kiszka We currently overflow due to wrong types used internally in rtc_mktime, on all platforms, and we return a too small type on 32-bit. One consumer that directly benefits from this is mktime64. Many others may still store the result in a wrong type. While at it, drop the redundant cast of mon in rtc_mktime (obsoleted by 714209832db1). Signed-off-by: Jan Kiszka =2D-- Noticed while playing with a x509 cert with 10000 days validity, resulting in a negative valid-to. include/linux/time.h | 3 --- include/rtc.h | 8 +++++--- lib/date.c | 13 +++++-------- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/include/linux/time.h b/include/linux/time.h index 702dd276aea..14ff5b6f481 100644 =2D-- a/include/linux/time.h +++ b/include/linux/time.h @@ -152,9 +152,6 @@ _DEFUN (ctime_r, (tim_p, result), return asctime_r (localtime_r (tim_p, &tm), result); } -/* for compatibility with linux code */ -typedef __s64 time64_t; - #ifdef CONFIG_LIB_DATE time64_t mktime64(const unsigned int year, const unsigned int mon, const unsigned int day, const unsigned int hour, diff --git a/include/rtc.h b/include/rtc.h index 6c7fcadd488..10104e3bf5a 100644 =2D-- a/include/rtc.h +++ b/include/rtc.h @@ -16,6 +16,8 @@ #include #include +typedef int64_t time64_t; + #ifdef CONFIG_DM_RTC struct udevice; @@ -301,7 +303,7 @@ int rtc_calc_weekday(struct rtc_time *time); void rtc_to_tm(u64 time_t, struct rtc_time *time); /** - * rtc_mktime() - Convert a broken-out time into a time_t value + * rtc_mktime() - Convert a broken-out time into a time64_t value * * The following fields need to be valid for this function to work: * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year @@ -309,9 +311,9 @@ void rtc_to_tm(u64 time_t, struct rtc_time *time); * Note that tm_wday and tm_yday are ignored. * * @time: Broken-out time to convert - * Return: corresponding time_t value, seconds since 1970-01-01 00:00:00 + * Return: corresponding time64_t value, seconds since 1970-01-01 00:00:0= 0 */ -unsigned long rtc_mktime(const struct rtc_time *time); +time64_t rtc_mktime(const struct rtc_time *time); /** * rtc_month_days() - The number of days in the month diff --git a/lib/date.c b/lib/date.c index c589d9ed3a2..e3d22459cd0 100644 =2D-- a/lib/date.c +++ b/lib/date.c @@ -71,19 +71,16 @@ int rtc_calc_weekday(struct rtc_time *tm) * -year / 100 + year / 400 terms, and add 10.] * * This algorithm was first published by Gauss (I think). - * - * WARNING: this function will overflow on 2106-02-07 06:28:16 on - * machines where long is 32-bit! (However, as time_t is signed, we - * will already get problems at other places on 2038-01-19 03:14:08) */ -unsigned long rtc_mktime(const struct rtc_time *tm) +time64_t rtc_mktime(const struct rtc_time *tm) { int mon =3D tm->tm_mon; int year =3D tm->tm_year; - int days, hours; + unsigned long days; + time64_t hours; mon -=3D 2; - if (0 >=3D (int)mon) { /* 1..12 -> 11, 12, 1..10 */ + if (0 >=3D mon) { /* 1..12 -> 11, 12, 1..10 */ mon +=3D 12; /* Puts Feb last since it has leap day */ year -=3D 1; } @@ -109,5 +106,5 @@ time64_t mktime64(const unsigned int year, const unsig= ned int mon, time.tm_min =3D min; time.tm_sec =3D sec; - return (time64_t)rtc_mktime((const struct rtc_time *)&time); + return rtc_mktime((const struct rtc_time *)&time); } =2D- 2.34.1