summaryrefslogtreecommitdiff
path: root/src/print.c
diff options
context:
space:
mode:
authorRichard M. Stallman <rms@gnu.org>2004-05-10 16:55:38 +0000
committerRichard M. Stallman <rms@gnu.org>2004-05-10 16:55:38 +0000
commitc11326718786ba5aa701fd9ba97c57876deab90e (patch)
tree39c5873c5097da0d2dc300a9f002e6dbf64abf6e /src/print.c
parent6740652e6d63701107312f7dd1efcc623c4b38fc (diff)
downloademacs-c11326718786ba5aa701fd9ba97c57876deab90e.tar.gz
(print_preprocess): Use being_printed, loop_count and
halftail to detect overdeep nesting and cyclic cdr chains.
Diffstat (limited to 'src/print.c')
-rw-r--r--src/print.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/print.c b/src/print.c
index e729b468fd1..27da81ae8eb 100644
--- a/src/print.c
+++ b/src/print.c
@@ -1280,6 +1280,26 @@ print_preprocess (obj)
{
int i;
EMACS_INT size;
+ int loop_count = 0;
+ Lisp_Object halftail;
+
+ /* Avoid infinite recursion for circular nested structure
+ in the case where Vprint_circle is nil. */
+ if (NILP (Vprint_circle))
+ {
+ for (i = 0; i < print_depth; i++)
+ if (EQ (obj, being_printed[i]))
+ return;
+ being_printed[print_depth] = obj;
+ }
+
+ /* Give up if we go so deep that print_object will get an error. */
+ /* See similar code in print_object. */
+ if (print_depth >= PRINT_CIRCLE)
+ return;
+
+ print_depth++;
+ halftail = obj;
loop:
if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
@@ -1340,8 +1360,15 @@ print_preprocess (obj)
break;
case Lisp_Cons:
+ /* Use HALFTAIL and LOOP_COUNT to detect circular lists,
+ just as in print_object. */
+ if (loop_count && EQ (obj, halftail))
+ break;
print_preprocess (XCAR (obj));
obj = XCDR (obj);
+ loop_count++;
+ if (!(loop_count & 1))
+ halftail = XCDR (halftail);
goto loop;
case Lisp_Vectorlike:
@@ -1356,6 +1383,7 @@ print_preprocess (obj)
break;
}
}
+ print_depth--;
}
static void
@@ -1426,6 +1454,7 @@ print_object (obj, printcharfun, escapeflag)
print_depth++;
+ /* See similar code in print_preprocess. */
if (print_depth > PRINT_CIRCLE)
error ("Apparently circular structure being printed");
#ifdef MAX_PRINT_CHARS