unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Adrià Garriga" <adria.garriga@gmail.com>
To: 37400@debbugs.gnu.org
Subject: bug#37400: Patch for bug in cl-reduce: function called with no arguments when list is empty
Date: Fri, 13 Sep 2019 09:50:39 +0100	[thread overview]
Message-ID: <18F2BFD9-81BA-4D56-8148-EC0FFA76A1AE@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 381 bytes --]

Hello Emacs developers,

I found a bug in cl-reduce, which should be reproducible by running the test in the patch. I'm running 26.2 but the bug is also present in master. Please let me know if there are any problems with the patch (including that it is useless :) or if I need to do something else.

Thank you for your hard work maintaining Emacs!

Adrià Garriga-Alonso


[-- Attachment #2: 0001-cl-reduce-avoids-calling-the-function-when-list-is-n.patch --]
[-- Type: application/octet-stream, Size: 2978 bytes --]

From 3c5df36904c8a5e9137a90309795b3eb9f90dd73 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A0=20Garriga-Alonso?= <adria.garriga@gmail.com>
Date: Wed, 4 Sep 2019 11:36:48 +0100
Subject: [PATCH] cl-reduce avoids calling the function when list is nil

---
 lisp/emacs-lisp/cl-seq.el            | 31 ++++++++++++++--------------
 test/lisp/emacs-lisp/cl-seq-tests.el |  6 ++++++
 2 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/lisp/emacs-lisp/cl-seq.el b/lisp/emacs-lisp/cl-seq.el
index a15c994bc1..8da6ec5a2a 100644
--- a/lisp/emacs-lisp/cl-seq.el
+++ b/lisp/emacs-lisp/cl-seq.el
@@ -134,21 +134,22 @@ If SEQ is empty, return :INITIAL-VALUE and FUNCTION is not
 called.
 
 \n(fn FUNCTION SEQ [KEYWORD VALUE]...)"
-  (cl--parsing-keywords (:from-end (:start 0) :end :initial-value :key) ()
-    (or (listp cl-seq) (setq cl-seq (append cl-seq nil)))
-    (setq cl-seq (cl-subseq cl-seq cl-start cl-end))
-    (if cl-from-end (setq cl-seq (nreverse cl-seq)))
-    (let ((cl-accum (cond ((memq :initial-value cl-keys) cl-initial-value)
-			  (cl-seq (cl--check-key (pop cl-seq)))
-			  (t (funcall cl-func)))))
-      (if cl-from-end
-	  (while cl-seq
-	    (setq cl-accum (funcall cl-func (cl--check-key (pop cl-seq))
-				    cl-accum)))
-	(while cl-seq
-	  (setq cl-accum (funcall cl-func cl-accum
-				  (cl--check-key (pop cl-seq))))))
-      cl-accum)))
+   (when cl-seq
+    (cl--parsing-keywords (:from-end (:start 0) :end :initial-value :key) ()
+      (or (listp cl-seq) (setq cl-seq (append cl-seq nil)))
+      (setq cl-seq (cl-subseq cl-seq cl-start cl-end))
+      (if cl-from-end (setq cl-seq (nreverse cl-seq)))
+      (let ((cl-accum (cond ((memq :initial-value cl-keys) cl-initial-value)
+                            (cl-seq (cl--check-key (pop cl-seq)))
+                            (t (funcall cl-func)))))
+        (if cl-from-end
+            (while cl-seq
+              (setq cl-accum (funcall cl-func (cl--check-key (pop cl-seq))
+                                      cl-accum)))
+          (while cl-seq
+            (setq cl-accum (funcall cl-func cl-accum
+                                    (cl--check-key (pop cl-seq))))))
+        cl-accum))))
 
 ;;;###autoload
 (defun cl-fill (cl-seq cl-item &rest cl-keys)
diff --git a/test/lisp/emacs-lisp/cl-seq-tests.el b/test/lisp/emacs-lisp/cl-seq-tests.el
index 6515eee9f2..56c1826483 100644
--- a/test/lisp/emacs-lisp/cl-seq-tests.el
+++ b/test/lisp/emacs-lisp/cl-seq-tests.el
@@ -311,5 +311,11 @@ Body are forms defining the test."
       (should (eq (cl-assoc x a) (car a)))
       (should (eq (cl-rassoc x a) (cadr a))))))
 
+(ert-deftest cl-seq-reduce ()
+  (let ((bad-add (lambda (x y) (+ x y))))
+    (should-not (cl-reduce bad-add nil))
+    (should (equal 1 (cl-reduce bad-add '(1))))
+    (should (equal 3 (cl-reduce bad-add '(1 2))))))
+
 (provide 'cl-seq-tests)
 ;;; cl-seq-tests.el ends here
-- 
2.21.0


             reply	other threads:[~2019-09-13  8:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-13  8:50 Adrià Garriga [this message]
2019-09-13 15:18 ` bug#37400: Patch for bug in cl-reduce: function called with no arguments when list is empty Noam Postavsky
2019-10-07  4:33   ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=18F2BFD9-81BA-4D56-8148-EC0FFA76A1AE@gmail.com \
    --to=adria.garriga@gmail.com \
    --cc=37400@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).