From: Alexandre Belloni <alexandre.belloni@bootlin.com>
To: Liu Dalin <liudalin@kylinsec.com.cn>
Cc: Dan Carpenter <error27@gmail.com>,
Hans Ulli Kroll <ulli.kroll@googlemail.com>,
Linus Walleij <linusw@kernel.org>,
linux-arm-kernel@lists.infradead.org, linux-rtc@vger.kernel.org,
linux-kernel@vger.kernel.org,
Deng Yingchao <dengyingchao@kylinsec.com.cn>,
Qin Yungao <qinyungao@kylinsec.com.cn>,
Luo Qiu <luoqiu@kylinsec.com.cn>
Subject: Re: [PATCH] rtc: ftrtc010: fix negative offset handling and smatch warning
Date: Thu, 8 Oct 2026 13:59:13 +0200 [thread overview]
Message-ID: <20261008115913bfd12cb4@mail.local> (raw)
In-Reply-To: <0C8AEB5636132B0C+20261008113732.581825-1-liudalin@kylinsec.com.cn>
On 08/10/2026 19:37:32+0800, Liu Dalin wrote:
> A prior attempted to fix the 32-bit arithmetic overflow by turning
> the implicit u32-to-timeu64_t cast into an explicit one. However,
> all calculations still happen in u32, inheriting the existing
> negative offset sign-extension bug, and the explicit cast triggers
> smatch "cast after binop" warning.
>
AI is lying to you, offset will never ever be negative.
> The FTRTC010 RECORD register stores a signed 32-bit two's complement
> offset exposed through readl() as a u32. Casting the whole u32
> expression result to timeu64_t performs zero extension, so negative
> offsets become huge positive values.
>
> Fix it by casting offset to s32 first to correctly interpret the
> two's complement sign, then promote all operands to timeu64_t so all
> calculations are done in 64-bit space. days/hour/min/sec are
> non-negative hardware counters, so promoting them directly to
> timeu64_t is safe.
>
> Fixes: 1d61d2592c1f ("rtc: gemini/ftrtc010: rename driver and symbols")
>
> Signed-off-by: Liu Dalin <liudalin@kylinsec.com.cn>
> ---
> drivers/rtc/rtc-ftrtc010.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/rtc/rtc-ftrtc010.c b/drivers/rtc/rtc-ftrtc010.c
> index b29c96be40f4..65fe1e872239 100644
> --- a/drivers/rtc/rtc-ftrtc010.c
> +++ b/drivers/rtc/rtc-ftrtc010.c
> @@ -72,7 +72,8 @@ static int ftrtc010_rtc_read_time(struct device *dev, struct rtc_time *tm)
> days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
> offset = readl(rtc->rtc_base + FTRTC010_RTC_RECORD);
>
> - time = (timeu64_t)(offset + days * 86400 + hour * 3600 + min * 60 + sec);
> + time = (timeu64_t)(s32)offset + (timeu64_t)days * 86400 +
> + (timeu64_t)hour * 3600 + (timeu64_t)min * 60 + (timeu64_t)sec;
>
> rtc_time64_to_tm(time, tm);
>
> --
> 2.43.0
>
--
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
next prev parent reply other threads:[~2026-10-08 11:59 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-08 11:37 Liu Dalin
2026-10-08 11:59 ` Alexandre Belloni [this message]
2026-10-08 12:24 ` liudalin
2026-10-08 13:07 ` Linus Walleij
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=20261008115913bfd12cb4@mail.local \
--to=alexandre.belloni@bootlin.com \
--cc=dengyingchao@kylinsec.com.cn \
--cc=error27@gmail.com \
--cc=linusw@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rtc@vger.kernel.org \
--cc=liudalin@kylinsec.com.cn \
--cc=luoqiu@kylinsec.com.cn \
--cc=qinyungao@kylinsec.com.cn \
--cc=ulli.kroll@googlemail.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
all inboxes | Powered by JetHome®