Hi Markus,

On 2026-05-11T12:09:09, Alexander Feilke
<[email protected]> wrote:
> cmd: date: validate date using rtc_month_days()
>
> The old check accepted day 0 as well as Feb 29th in non-leap years.
> With this change, both day and month 0 are rejected, and the local day limit
> logic is now handled by rtc_month_days(), which correctly accounts for month
> length and leap years.
>
> Because of this, special attention must be taken in the 'MMDDhhmm' format 
> case,
> as tm_year is not initialized. The leap-year calculation in rtc_month_days()
> therefore depends on the value provided by the caller. This is pre-existing
> behaviour, but is now made more explicit.
>
> Signed-off-by: Markus Niebel <[email protected]>
> Signed-off-by: Alexander Feilke <[email protected]>
>
> cmd/date.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)

> diff --git a/cmd/date.c b/cmd/date.c
> @@ -167,12 +167,13 @@ int mk_date (const char *datestr, struct rtc_time *tmp)
>               /* fall thru */
>       case 12:                /* MMDDhhmmCCYY */
>               if (cnvrt2 (datestr+0, &val) ||
> -                 val > 12) {
> +                 val > 12 || val < 1) {
>                       break;
>               }
>               tmp->tm_mon  = val;
> -             if (cnvrt2 (datestr+2, &val) ||
> -                 val > ((tmp->tm_mon==2) ? 29 : 31)) {
> +             if (cnvrt2(datestr+2, &val) ||
> +                 val < 1 ||
> +                 val > rtc_month_days(tmp->tm_mon - 1, tmp->tm_year)) {
>                       break;
>               }
>               tmp->tm_mday = val;


The commit message says tm_year is not initialised in the MMDDhhmm
case, but do_date() calls dm_rtc_get() into the same struct before
mk_date(), so tm_year holds the current RTC year on entry. That is
what makes the leap-year check work. You could rephrase to something
like 'tm_year is not set by mk_date() in this path, so the leap-year
check relies on the caller having pre-populated it (do_date() does,
via dm_rtc_get())'.

I wonder if mk_date() could be static?

Reviewed-by: Simon Glass <[email protected]>

Regards,
Simon

Reply via email to