From ac288e15092bad38c2a13d36908def87258a63af Mon Sep 17 00:00:00 2001 From: Richard Lawrence Date: Fri, 13 Dec 2024 10:41:02 +0100 Subject: [PATCH] Check for presuppositions in `calendar-date-is-valid-p' Do not signal an error in `calendar-date-is-valid-p' if passed a value which is not a three-element list of integers. Signaling an error makes the function unusable as a predicate for valid date values. (Bug#74848) * lisp/calendar/calendar.el (calendar-date-is-valid-p): Add the check that input is a 3-element list. * test/lisp/calendar/calendar-tests.el: New file with tests. (cherry picked from commit 77f73abd92f1a627ddec0648dcfe14e56b525f4d) --- lisp/calendar/calendar.el | 29 +++++++++++++----------- test/lisp/calendar/calendar-tests.el | 34 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 test/lisp/calendar/calendar-tests.el diff --git a/lisp/calendar/calendar.el b/lisp/calendar/calendar.el index 8cc35e48b5b..22fab0c242f 100644 --- a/lisp/calendar/calendar.el +++ b/lisp/calendar/calendar.el @@ -2460,19 +2460,22 @@ Returns the corresponding Gregorian date." (defun calendar-date-is-valid-p (date) "Return t if DATE is a valid date." - (let ((month (calendar-extract-month date)) - (day (calendar-extract-day date)) - (year (calendar-extract-year date))) - (and (<= 1 month) (<= month 12) - ;; (calendar-read-date t) used to return a date with day = nil. - ;; Should not be valid (?), since many funcs prob assume integer. - ;; (calendar-read-date 'noday) returns (month year), which - ;; currently results in calendar-extract-year returning nil. - day year (<= 1 day) (<= day (calendar-last-day-of-month month year)) - ;; BC dates left as non-valid, to suppress errors from - ;; complex holiday algorithms not suitable for years BC. - ;; Note there are side effects on calendar navigation. - (<= 1 year)))) + (when (and (listp date) + (length= date 3)) + (let ((month (calendar-extract-month date)) + (day (calendar-extract-day date)) + (year (calendar-extract-year date))) + (and (integerp month) (integerp day) (integerp year) + (<= 1 month) (<= month 12) + ;; (calendar-read-date t) used to return a date with day = nil. + ;; Should not be valid (?), since many funcs prob assume integer. + ;; (calendar-read-date 'noday) returns (month year), which + ;; currently results in calendar-extract-year returning nil. + day year (<= 1 day) (<= day (calendar-last-day-of-month month year)) + ;; BC dates left as non-valid, to suppress errors from + ;; complex holiday algorithms not suitable for years BC. + ;; Note there are side effects on calendar navigation. + (<= 1 year))))) (defun calendar-date-equal (date1 date2) "Return t if the DATE1 and DATE2 are the same." diff --git a/test/lisp/calendar/calendar-tests.el b/test/lisp/calendar/calendar-tests.el new file mode 100644 index 00000000000..c41f14d3b54 --- /dev/null +++ b/test/lisp/calendar/calendar-tests.el @@ -0,0 +1,34 @@ +;;; calendar-tests.el --- tests for calendar/calendar.el -*- lexical-binding:t -*- + +;; Copyright (C) 2024 Free Software Foundation, Inc. + +;; Author: Richard Lawrence + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Code: + +(require 'ert) +(require 'calendar) + +(ert-deftest calendar-test-validity-predicate () + (should (eq (calendar-date-is-valid-p nil) nil)) + (should (eq (calendar-date-is-valid-p "invalid") nil)) + (should (eq (calendar-date-is-valid-p (list 1 2)) nil)) + (should (eq (calendar-date-is-valid-p (list 5 1 2025)) t))) + +(provide 'calendar-tests) +;;; calendar-tests.el ends here -- 2.39.2