Fix infloop in 'number-sequence'
* lisp/subr.el (number-sequence): Avoid overflow leading to an infloop. (Bug#23627) * test/automated/subr-tests.el (number-sequence-test): New test.
This commit is contained in:
parent
4ab2673d87
commit
f7ffc4b7d3
2 changed files with 15 additions and 3 deletions
|
@ -484,13 +484,16 @@ of course, also replace TO with a slightly larger value
|
||||||
(list from)
|
(list from)
|
||||||
(or inc (setq inc 1))
|
(or inc (setq inc 1))
|
||||||
(when (zerop inc) (error "The increment can not be zero"))
|
(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)
|
(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)
|
(setq seq (cons next seq)
|
||||||
n (1+ n)
|
n (1+ n)
|
||||||
|
last next
|
||||||
next (+ from (* n inc))))
|
next (+ from (* n inc))))
|
||||||
(while (>= next to)
|
(while (and (<= next last) (>= next to))
|
||||||
(setq seq (cons next seq)
|
(setq seq (cons next seq)
|
||||||
n (1+ n)
|
n (1+ n)
|
||||||
next (+ from (* n inc)))))
|
next (+ from (* n inc)))))
|
||||||
|
|
|
@ -61,6 +61,15 @@
|
||||||
(quote
|
(quote
|
||||||
(0 font-lock-keyword-face))))))))
|
(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 ()
|
(ert-deftest string-comparison-test ()
|
||||||
(should (string-lessp "abc" "acb"))
|
(should (string-lessp "abc" "acb"))
|
||||||
(should (string-lessp "aBc" "abc"))
|
(should (string-lessp "aBc" "abc"))
|
||||||
|
|
Loading…
Add table
Reference in a new issue