Fix up length_internal with degenerate length inputs

* src/fns.c (length_internal): Protect against edge conditions.
This commit is contained in:
Lars Ingebrigtsen 2020-12-27 09:22:02 +01:00
parent 80420faf49
commit eb98afaf35
2 changed files with 16 additions and 2 deletions

View file

@ -159,14 +159,14 @@ EMACS_INT length_internal (Lisp_Object sequence, int len)
if (len < 0xffff)
while (CONSP (sequence))
{
if (--len == 0)
if (--len <= 0)
return -1;
sequence = XCDR (sequence);
}
/* Signal an error on circular lists. */
else
FOR_EACH_TAIL (sequence)
if (--len == 0)
if (--len <= 0)
return -1;
return len;
}
@ -210,6 +210,9 @@ counted. */)
CHECK_FIXNUM (length);
EMACS_INT len = XFIXNUM (length);
if (len < 0)
return Qnil;
if (CONSP (sequence))
return length_internal (sequence, len + 1) == 1? Qt: Qnil;
else

View file

@ -1025,6 +1025,17 @@
(should (length= "abc" 3))
(should-not (length= "abc" 4))
(should-not (length< (list 1 2 3) -1))
(should-not (length< (list 1 2 3) 0))
(should-not (length< (list 1 2 3) -10))
(should (length> (list 1 2 3) -1))
(should (length> (list 1 2 3) 0))
(should-not (length= (list 1 2 3) -1))
(should-not (length= (list 1 2 3) 0))
(should-not (length= (list 1 2 3) 1))
(should-error
(let ((list (list 1)))
(setcdr list list)