Subject: bug#25477: 26.0.50; `dolist' macro accepts more than three arguments

From: Philipp Stephani <[email-masked]>
Date: Sun, 26 Mar 2017 18:56:39 +0000

Philipp Stephani <[email-masked]> schrieb am Mi., 18. Jan. 2017 um
22:37 Uhr:

>
> In *scratch*, evaluate
>
> (dolist (a () 1 2 3))
>
> The result is 3, but a `wrong-number-of-arguments' signal should be
> raised.
>

I've attached a patch.
From cb1f8ce80edf1de7a1f9b886308a688a2ab6dcbf Mon Sep 17 00:00:00 2001
From: Philipp Stephani <[email-masked]>
Date: Sun, 26 Mar 2017 20:53:43 +0200
Subject: [PATCH] Validate SPEC of `dolist', cf. Bug#25477.

* lisp/subr.el (dolist): Test type and length of SPEC.
* test/lisp/subr-tests.el (subr-tests--dolist--wrong-number-of-args):
Add unit test.
---
 lisp/subr.el            |  4 ++++
 test/lisp/subr-tests.el | 10 ++++++++++
 2 files changed, 14 insertions(+)

diff --git a/lisp/subr.el b/lisp/subr.el
index 6b0403890c..65d1b9b482 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -190,6 +190,10 @@ dolist
 
 \(fn (VAR LIST [RESULT]) BODY...)"
   (declare (indent 1) (debug ((symbolp form &optional form) body)))
+  (unless (consp spec)
+    (signal 'wrong-type-argument (list 'consp spec)))
+  (unless (<= 2 (length spec) 3)
+    (signal 'wrong-number-of-arguments (list '(2 . 3) (length spec))))
   ;; It would be cleaner to create an uninterned symbol,
   ;; but that uses a lot more space when many functions in many files
   ;; use dolist.
diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el
index a3b08e9697..0d243cc5d8 100644
--- a/test/lisp/subr-tests.el
+++ b/test/lisp/subr-tests.el
@@ -281,5 +281,15 @@ subr-test--frames-1
   (should (equal (string-match-p "\\`[[:blank:]]\\'" "\u3000") 0))
   (should-not (string-match-p "\\`[[:blank:]]\\'" "\N{LINE SEPARATOR}")))
 
+(ert-deftest subr-tests--dolist--wrong-number-of-args ()
+  "Test that `dolist' doesn't accept wrong types or length of SPEC,
+cf. Bug#25477."
+  (should-error (eval '(dolist (a)))
+                :type 'wrong-number-of-arguments)
+  (should-error (eval '(dolist (a () 'result 'invalid)) t)
+                :type 'wrong-number-of-arguments)
+  (should-error (eval '(dolist "foo") t)
+                :type 'wrong-type-argument))
+
 (provide 'subr-tests)
 ;;; subr-tests.el ends here
-- 
2.12.2