Re: [PATCH v3 3/5] rtc: Fix the RTC time comparison issues adding cast

Alexandre Mergnat
2025-04-14T10:46:14Z

On 11/04/2025 15:38, Alexandre Belloni wrote:
> On 11/04/2025 14:35:56+0200, Alexandre Mergnat wrote:
>> The RTC subsystem was experiencing comparison issues between signed and
>> unsigned time values. When comparing time64_t variables (signed) with
>> potentially unsigned range values, incorrect results could occur leading
>> to runtime errors.
>>
>> Adds explicit type casts to time64_t for critical RTC time comparisons
>> in both class.c and interface.c files. The changes ensure proper
>> handling of negative time values during range validation and offset
>> calculations, particularly when dealing with timestamps before 1970.
>>
>> The previous implementation might incorrectly interpret negative values
>> as extremely large positive values, causing unexpected behavior in the
>> RTC hardware abstraction logic.
>>
> range_max is explicitly unsigned, casting it to a signed value will
> break drivers.

Ok, It should be fine for all drivers using range_max =
U32_MAX
RTC_TIMESTAMP_END_2099
RTC_TIMESTAMP_END_9999
(1 << 14) * 86400ULL - 1

Whereas drivers using range_max = U64_MAX going in trouble:
rtc-goldfish.c
rtc-ps3.c
rtc-st-lpc.c
rtc-sun4v.c

Is it ok for you if I fix the drivers to avoid issue with signed range_max ? Because, at the end,
you can't keep comparison operations between signed and unsigned variable, it lead to future issues.

Otherwise, I've another working implementation which remove all comparison operation and drivers
doesn't require to be modify.

--
Regards,
Alexandre