2013-12-04 Stefan Monnier <monnier@iro.umontreal.ca>
+ * lisp.h (FOR_EACH_TAIL): New macro.
+ * fns.c (Fdelq): Use it to avoid inf-loops; remove QUIT.
+
* window.c (select_window): Call second wset_redisplay before we change
selected_window (bug#16034).
the value of a list `foo'. */)
(register Lisp_Object elt, Lisp_Object list)
{
- register Lisp_Object tail, prev;
- register Lisp_Object tem;
+ Lisp_Object tail, tortoise, prev = Qnil;
+ bool skip;
- tail = list;
- prev = Qnil;
- while (CONSP (tail))
+ FOR_EACH_TAIL (tail, list, tortoise, skip)
{
- CHECK_LIST_CONS (tail, list);
- tem = XCAR (tail);
+ Lisp_Object tem = XCAR (tail);
if (EQ (elt, tem))
{
if (NILP (prev))
}
else
prev = tail;
- tail = XCDR (tail);
- QUIT;
}
return list;
}
memory_full (SIZE_MAX); \
} while (0)
+/* Loop over all tails of a list, checking for cycles.
+ FIXME: Make tortoise and n internal declarations.
+ FIXME: Unroll the loop body so we don't need `n'. */
+#define FOR_EACH_TAIL(hare, list, tortoise, n) \
+ for (tortoise = hare = (list), n = true; \
+ CONSP (hare); \
+ (hare = XCDR (hare), n = !n, \
+ (n \
+ ? ((EQ (hare, tortoise) \
+ && (xsignal1 (Qcircular_list, (list)), 0))) \
+ /* Move tortoise before the next iteration, in case */ \
+ /* the next iteration does an Fsetcdr. */ \
+ : (tortoise = XCDR (tortoise), 0))))
+
/* Do a `for' loop over alist values. */
#define FOR_EACH_ALIST_VALUE(head_var, list_var, value_var) \