]> code.delx.au - gnu-emacs/commitdiff
Fix infloop in 'number-sequence'
authorEli Zaretskii <eliz@gnu.org>
Fri, 27 May 2016 09:17:15 +0000 (12:17 +0300)
committerEli Zaretskii <eliz@gnu.org>
Fri, 27 May 2016 09:17:15 +0000 (12:17 +0300)
* lisp/subr.el (number-sequence): Avoid overflow leading to an
infloop.  (Bug#23627)

* test/automated/subr-tests.el (number-sequence-test): New test.

lisp/subr.el
test/automated/subr-tests.el

index 3ac61f9a45f6f96d02015c90fc1ab578fa1ec24c..43660d74377c8f80d1b71dbfc56445c6ce9c7497 100644 (file)
@@ -484,13 +484,16 @@ of course, also replace TO with a slightly larger value
       (list from)
     (or inc (setq inc 1))
     (when (zerop inc) (error "The increment can not be zero"))
-    (let (seq (n 0) (next from))
+    (let (seq (n 0) (next from) (last from))
       (if (> inc 0)
-          (while (<= next to)
+          ;; The (>= next last) condition protects against integer
+          ;; overflow in computing NEXT.
+          (while (and (>= next last) (<= next to))
             (setq seq (cons next seq)
                   n (1+ n)
+                  last next
                   next (+ from (* n inc))))
-        (while (>= next to)
+        (while (and (<= next last) (>= next to))
           (setq seq (cons next seq)
                 n (1+ n)
                 next (+ from (* n inc)))))
index 7906a207a96ac6f0eee7d32861fe4d12873cd20e..ce212903c9d3cb41deb4c52d97d0305e4a49a0ec 100644 (file)
                      (quote
                       (0 font-lock-keyword-face))))))))
 
+(ert-deftest number-sequence-test ()
+  (should (= (length
+              (number-sequence (1- most-positive-fixnum) most-positive-fixnum))
+             2))
+  (should (= (length
+              (number-sequence
+               (1+ most-negative-fixnum) most-negative-fixnum -1))
+             2)))
+
 (ert-deftest string-comparison-test ()
   (should (string-lessp "abc" "acb"))
   (should (string-lessp "aBc" "abc"))