unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#19434: 24.4; cl-subseq bounding issues
@ 2014-12-24  4:14 Leo Liu
  2015-01-01 16:13 ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Leo Liu @ 2014-12-24  4:14 UTC (permalink / raw)
  To: 19434


At the moment (cl-subseq '(1 2 3) 0 5) => (1 2 3 nil nil)

I propose the following fix:

diff --git a/lisp/emacs-lisp/cl-extra.el b/lisp/emacs-lisp/cl-extra.el
index a94dcd33..b50c1e6a 100644
--- a/lisp/emacs-lisp/cl-extra.el
+++ b/lisp/emacs-lisp/cl-extra.el
@@ -524,15 +524,21 @@ (defun cl-subseq (seq start &optional end)
               `(progn (cl-replace ,seq ,new :start1 ,start :end1 ,end)
                       ,new))))
   (if (stringp seq) (substring seq start end)
-    (let (len)
+    (let ((orig-start start) (orig-end end) len)
       (and end (< end 0) (setq end (+ end (setq len (length seq)))))
       (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
       (cond ((listp seq)
-	     (if (> start 0) (setq seq (nthcdr start seq)))
+	     (when (> start 0)
+	       (setq seq (nthcdr (1- start) seq))
+	       (or seq
+		   (error "Bad bounding indices: %s, %s" orig-start orig-end))
+	       (setq seq (cdr seq)))
 	     (if end
 		 (let ((res nil))
-		   (while (>= (setq end (1- end)) start)
+		   (while (and seq (>= (setq end (1- end)) start))
 		     (push (pop seq) res))
+		   (or (= start end)
+		       (error "Bad bounding indices: %s, %s" orig-start orig-end))
 		   (nreverse res))
 	       (copy-sequence seq)))
 	    (t
diff --git a/test/automated/cl-lib-tests.el b/test/automated/cl-lib-tests.el
index e4c6e914..162f7aeb 100644
--- a/test/automated/cl-lib-tests.el
+++ b/test/automated/cl-lib-tests.el
@@ -245,4 +245,10 @@ (ert-deftest cl-parse-integer ()
 (ert-deftest cl-loop-destructuring-with ()
   (should (equal (cl-loop with (a b c) = '(1 2 3) return (+ a b c)) 6)))
 
+(ert-deftest cl-subseq ()
+  (should-error (cl-subseq '(1 2 3) 4))
+  (should-not   (cl-subseq '(1 2 3) 3))
+  (should       (cl-subseq '(1 2 3) -3))
+  (should-error (cl-subseq '(1 2 3) 1 4)))
+
 ;;; cl-lib.el ends here





^ permalink raw reply related	[flat|nested] 4+ messages in thread

* bug#19434: 24.4; cl-subseq bounding issues
  2014-12-24  4:14 bug#19434: 24.4; cl-subseq bounding issues Leo Liu
@ 2015-01-01 16:13 ` Stefan Monnier
  2015-01-04 23:05   ` Leo Liu
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2015-01-01 16:13 UTC (permalink / raw)
  To: Leo Liu; +Cc: 19434

> At the moment (cl-subseq '(1 2 3) 0 5) => (1 2 3 nil nil)
> I propose the following fix:

Fine by me.  Can you check if seq-subseq (in seq.el) needs a similar fix?
And/or if cl-subseq can make use of seq-subseq?


        Stefan





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#19434: 24.4; cl-subseq bounding issues
  2015-01-01 16:13 ` Stefan Monnier
@ 2015-01-04 23:05   ` Leo Liu
  2015-01-18  6:21     ` Leo Liu
  0 siblings, 1 reply; 4+ messages in thread
From: Leo Liu @ 2015-01-04 23:05 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 19434

On 2015-01-02 03:13 +1100, Stefan Monnier wrote:
> Fine by me.  Can you check if seq-subseq (in seq.el) needs a similar fix?
> And/or if cl-subseq can make use of seq-subseq?

I am on the road but I'll take care of this soonish.

Leo





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#19434: 24.4; cl-subseq bounding issues
  2015-01-04 23:05   ` Leo Liu
@ 2015-01-18  6:21     ` Leo Liu
  0 siblings, 0 replies; 4+ messages in thread
From: Leo Liu @ 2015-01-18  6:21 UTC (permalink / raw)
  To: 19434-done

Version: 25.1





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-01-18  6:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-24  4:14 bug#19434: 24.4; cl-subseq bounding issues Leo Liu
2015-01-01 16:13 ` Stefan Monnier
2015-01-04 23:05   ` Leo Liu
2015-01-18  6:21     ` Leo Liu

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).