[PATCH v3 2/9] cmd: date: validate date using rtc_month_days()

Alexander Feilke Alexander.Feilke at ew.tq-group.com
Fri May 22 17:39:22 CEST 2026


From: Markus Niebel <Markus.Niebel at ew.tq-group.com>

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.

In the 'MMDDhhmm' format case, tm_year is not initialized by mk_date().
The leap-year calculation in rtc_month_days() therefore depends on the
value provided by the caller, which do_date() does, via dm_rtc_get().
This is pre-existing behaviour, but is now made more explicit.

Signed-off-by: Markus Niebel <Markus.Niebel at ew.tq-group.com>
Reviewed-by: Simon Glass <sjg at chromium.org>
Reviewed-by: Alexander Sverdlin <alexander.sverdlin at siemens.com>
Signed-off-by: Alexander Feilke <alexander.feilke at ew.tq-group.com>
---
 cmd/date.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/cmd/date.c b/cmd/date.c
index f7ecdac7dd3..67cfaed0d48 100644
--- a/cmd/date.c
+++ b/cmd/date.c
@@ -167,12 +167,13 @@ static 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;
-- 
2.34.1



More information about the U-Boot mailing list