bug#40217: 28.0.50; Add check for valid month to date-days-in-month

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

bug#40217: 28.0.50; Add check for valid month to date-days-in-month

Alex Branham
Hello -

Here's a patch that adds a check for whether the month argument is
valid in the new date-days-in-month function. It currently returns 30
for a month value of, say, 15.

Thanks,
Alex


From f9c3040d516314209d64738fd23deafc5eb59faa Mon Sep 17 00:00:00 2001
From: Alex Branham <[hidden email]>
Date: Tue, 24 Mar 2020 19:34:14 -0400
Subject: [PATCH] Add check for valid month to date-days-in-month

* lisp/calendar/time-date.el (date-days-in-month): Add check.
* test/lisp/calendar/time-date-tests.el (test-days-in-month): Add test
for new error.
---
 lisp/calendar/time-date.el            | 2 ++
 test/lisp/calendar/time-date-tests.el | 3 ++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/lisp/calendar/time-date.el b/lisp/calendar/time-date.el
index e2402de801..9b58a4884b 100644
--- a/lisp/calendar/time-date.el
+++ b/lisp/calendar/time-date.el
@@ -355,6 +355,8 @@ seconds-to-string
 
 (defun date-days-in-month (year month)
   "The number of days in MONTH in YEAR."
+  (unless (<= 1 month 12)
+    (error "Month %s invalid" month))
   (if (= month 2)
       (if (date-leap-year-p year)
           29
diff --git a/test/lisp/calendar/time-date-tests.el b/test/lisp/calendar/time-date-tests.el
index 4c8f18a7a9..9c90300cfe 100644
--- a/test/lisp/calendar/time-date-tests.el
+++ b/test/lisp/calendar/time-date-tests.el
@@ -31,7 +31,8 @@ test-leap-year
 (ert-deftest test-days-in-month ()
   (should (= (date-days-in-month 2004 2) 29))
   (should (= (date-days-in-month 2004 3) 31))
-  (should-not (= (date-days-in-month 1900 3) 28)))
+  (should-not (= (date-days-in-month 1900 3) 28))
+  (should-error (date-days-in-month 2020 15)))
 
 (ert-deftest test-ordinal ()
   (should (equal (date-ordinal-to-time 2008 271)
--
2.25.1

Reply | Threaded
Open this post in threaded view
|

bug#40217: 28.0.50; Add check for valid month to date-days-in-month

Eli Zaretskii
> From: Alex Branham <[hidden email]>
> Date: Tue, 24 Mar 2020 19:39:44 -0400
>
> Here's a patch that adds a check for whether the month argument is
> valid in the new date-days-in-month function. It currently returns 30
> for a month value of, say, 15.

Thanks, pushed to the master branch (after some minor fixes of the
commit log message).