Subject: Re: bug#17340: 24.4.50; emacs_backtrace.txt

From: Stefan Monnier <[email-masked]>
Date: Fri, 25 Apr 2014 12:40:08 -0400

>>>>> "Eli" == Eli Zaretskii <[email-masked]> writes:

>> From: Juanma Barranquero <[email-masked]>
>> Date: Fri, 25 Apr 2014 10:08:18 +0200
>> Cc: [email-masked]
>> 
>> w32_backtrace at w32fns.c:8476
>> emacs_abort at w32fns.c:8508
>> terminate_due_to_signal at emacs.c:382
>> die at alloc.c:6955
>> XVECTOR at lisp.h:821
>> AREF at lisp.h:1308
>> coding_inherit_eol_type at coding.c:6031
>> Fchoose_write_coding_system at fileio.c:4632
>> write_region at fileio.c:4802
>> Fwrite_region at fileio.c:4676
>> auto_save_1 at fileio.c:5456
>> internal_condition_case at eval.c:1354
>> Fdo_auto_save at fileio.c:5666
>> shut_down_emacs at emacs.c:2033
>> terminate_due_to_signal at emacs.c:366
>> die at alloc.c:6955
>> rotate_left at intervals.c:393  <<<<<<<<<<<<<<<<<<<<<<<
>> balance_an_interval at intervals.c:455

> This one's definitely related to Stefan's latest changes.

Could you run with the patch below (and enable_checking, obviously), to
try and catch the problem sooner?

        Stefan

=== modified file 'src/intervals.c'
--- src/intervals.c	2014-04-19 18:13:26 +0000
+++ src/intervals.c	2014-04-25 16:38:44 +0000
@@ -110,13 +110,14 @@
       set_string_intervals (parent, new);
       new->position = 0;
     }
+  eassert (LENGTH (new) > 0);
 
   set_interval_object (new, parent);
 
   return new;
 }
 
-/* Make the interval TARGET have exactly the properties of SOURCE */
+/* Make the interval TARGET have exactly the properties of SOURCE.  */
 
 void
 copy_properties (register INTERVAL source, register INTERVAL target)
@@ -176,10 +177,10 @@
   Lisp_Object i1_cdr, i1_val;
 
   if (DEFAULT_INTERVAL_P (i0) && DEFAULT_INTERVAL_P (i1))
-    return 1;
+    return true;
 
   if (DEFAULT_INTERVAL_P (i0) || DEFAULT_INTERVAL_P (i1))
-    return 0;
+    return false;
 
   i0_cdr = i0->plist;
   i1_cdr = i1->plist;
@@ -188,31 +189,31 @@
       i0_sym = XCAR (i0_cdr);
       i0_cdr = XCDR (i0_cdr);
       if (!CONSP (i0_cdr))
-	return 0;
+	return false;
       i1_val = i1->plist;
       while (CONSP (i1_val) && !EQ (XCAR (i1_val), i0_sym))
 	{
 	  i1_val = XCDR (i1_val);
 	  if (!CONSP (i1_val))
-	    return 0;
+	    return false;
 	  i1_val = XCDR (i1_val);
 	}
 
       /* i0 has something i1 doesn't.  */
       if (EQ (i1_val, Qnil))
-	return 0;
+	return false;
 
       /* i0 and i1 both have sym, but it has different values in each.  */
       if (!CONSP (i1_val)
 	  || (i1_val = XCDR (i1_val), !CONSP (i1_val))
 	  || !EQ (XCAR (i1_val), XCAR (i0_cdr)))
-	return 0;
+	return false;
 
       i0_cdr = XCDR (i0_cdr);
 
       i1_cdr = XCDR (i1_cdr);
       if (!CONSP (i1_cdr))
-	return 0;
+	return false;
       i1_cdr = XCDR (i1_cdr);
     }
 
@@ -339,10 +340,8 @@
   ptrdiff_t old_total = A->total_length;
 
   eassert (old_total > 0);
-  eassert (old_total
-	   > TOTAL_LENGTH (B) + TOTAL_LENGTH (A->right));
-  eassert (TOTAL_LENGTH (B)
-	   > TOTAL_LENGTH (B->left) + TOTAL_LENGTH (c));
+  eassert (LENGTH (A) > 0);
+  eassert (LENGTH (B) > 0);
 
   /* Deal with any Parent of A;  make it point to B.  */
   if (! ROOT_INTERVAL_P (A))
@@ -366,9 +365,11 @@
   /* A's total length is decreased by the length of B and its left child.  */
   A->total_length -= B->total_length - TOTAL_LENGTH (c);
   eassert (TOTAL_LENGTH (A) > 0);
+  eassert (LENGTH (A) > 0);
 
   /* B must have the same total length of A.  */
   B->total_length = old_total;
+  eassert (LENGTH (B) > 0);
 
   return B;
 }
@@ -390,10 +391,8 @@
   ptrdiff_t old_total = A->total_length;
 
   eassert (old_total > 0);
-  eassert (old_total
-	   > TOTAL_LENGTH (B) + TOTAL_LENGTH (A->left));
-  eassert (TOTAL_LENGTH (B)
-	   > TOTAL_LENGTH (B->right) + TOTAL_LENGTH (c));
+  eassert (LENGTH (A) > 0);
+  eassert (LENGTH (B) > 0);
 
   /* Deal with any parent of A;  make it point to B.  */
   if (! ROOT_INTERVAL_P (A))
@@ -417,9 +416,11 @@
   /* A's total length is decreased by the length of B and its right child.  */
   A->total_length -= B->total_length - TOTAL_LENGTH (c);
   eassert (TOTAL_LENGTH (A) > 0);
+  eassert (LENGTH (A) > 0);
 
   /* B must have the same total length of A.  */
   B->total_length = old_total;
+  eassert (LENGTH (B) > 0);
 
   return B;
 }
@@ -432,6 +433,9 @@
 {
   register ptrdiff_t old_diff, new_diff;
 
+  eassert (LENGTH (i) > 0);
+  eassert (TOTAL_LENGTH (i) >= LENGTH (i));
+
   while (1)
     {
       old_diff = LEFT_TOTAL_LENGTH (i) - RIGHT_TOTAL_LENGTH (i);
@@ -468,16 +472,16 @@
 balance_possible_root_interval (INTERVAL interval)
 {
   Lisp_Object parent;
-  bool have_parent = 0;
-
-  if (!INTERVAL_HAS_OBJECT (interval) && !INTERVAL_HAS_PARENT (interval))
-    return interval;
+  bool have_parent = false;
 
   if (INTERVAL_HAS_OBJECT (interval))
     {
-      have_parent = 1;
+      have_parent = true;
       GET_INTERVAL_OBJECT (parent, interval);
     }
+  else if (!INTERVAL_HAS_PARENT (interval))
+    return interval;
+
   interval = balance_an_interval (interval);
 
   if (have_parent)
@@ -553,7 +557,7 @@
     {
       set_interval_right (interval, new);
       new->total_length = new_length;
-      eassert (TOTAL_LENGTH (new) >= 0);
+      eassert (LENGTH (new) > 0);
     }
   else
     {
@@ -562,7 +566,6 @@
       set_interval_parent (interval->right, new);
       set_interval_right (interval, new);
       new->total_length = new_length + new->right->total_length;
-      eassert (TOTAL_LENGTH (new) >= 0);
       balance_an_interval (new);
     }
 
@@ -598,7 +601,7 @@
     {
       set_interval_left (interval, new);
       new->total_length = new_length;
-      eassert (TOTAL_LENGTH (new) >= 0);
+      eassert (LENGTH (new) > 0);
     }
   else
     {
@@ -607,7 +610,6 @@
       set_interval_parent (new->left, new);
       set_interval_left (interval, new);
       new->total_length = new_length + new->left->total_length;
-      eassert (TOTAL_LENGTH (new) >= 0);
       balance_an_interval (new);
     }
 
@@ -796,7 +798,7 @@
 	    {
 	      i->left->position = i->position - TOTAL_LENGTH (i->left)
 		+ LEFT_TOTAL_LENGTH (i->left);
-	      i = i->left;		/* Move to the left child */
+	      i = i->left;		/* Move to the left child.  */
 	    }
 	  else if (NULL_PARENT (i))
 	    error ("Point before start of properties");
@@ -811,7 +813,7 @@
 	    {
 	      i->right->position = INTERVAL_LAST_POS (i)
 	        + LEFT_TOTAL_LENGTH (i->right);
-	      i = i->right;		/* Move to the right child */
+	      i = i->right;		/* Move to the right child.  */
 	    }
 	  else if (NULL_PARENT (i))
 	    error ("Point %"pD"d after end of properties", pos);
@@ -958,7 +960,6 @@
       for (temp = prev ? prev : i; temp; temp = INTERVAL_PARENT_OR_NULL (temp))
 	{
 	  temp->total_length += length;
-	  eassert (TOTAL_LENGTH (temp) >= 0);
 	  temp = balance_possible_root_interval (temp);
 	}
 
@@ -1014,7 +1015,6 @@
       for (temp = i; temp; temp = INTERVAL_PARENT_OR_NULL (temp))
 	{
 	  temp->total_length += length;
-	  eassert (TOTAL_LENGTH (temp) >= 0);
 	  temp = balance_possible_root_interval (temp);
 	}
     }
@@ -1216,9 +1216,10 @@
       this = this->left;
       this->total_length += migrate_amt;
     }
-  eassert (TOTAL_LENGTH (this) >= 0);
   set_interval_left (this, migrate);
   set_interval_parent (migrate, this);
+  eassert (LENGTH (this) > 0);
+  eassert (LENGTH (i->right) > 0);
 
   return i->right;
 }
@@ -1298,7 +1299,7 @@
 							 relative_position,
 							 amount);
       tree->total_length -= subtract;
-      eassert (TOTAL_LENGTH (tree) >= 0);
+      eassert (LENGTH (tree) > 0);
       return subtract;
     }
   /* Right branch.  */
@@ -1313,7 +1314,7 @@
 					       relative_position,
 					       amount);
       tree->total_length -= subtract;
-      eassert (TOTAL_LENGTH (tree) >= 0);
+      eassert (LENGTH (tree) > 0);
       return subtract;
     }
   /* Here -- this node.  */
@@ -1328,7 +1329,7 @@
 	amount = my_amount;
 
       tree->total_length -= amount;
-      eassert (TOTAL_LENGTH (tree) >= 0);
+      eassert (LENGTH (tree) >= 0);
       if (LENGTH (tree) == 0)
 	delete_interval (tree);
 
@@ -1370,7 +1371,7 @@
   if (ONLY_INTERVAL_P (tree))
     {
       tree->total_length -= length;
-      eassert (TOTAL_LENGTH (tree) >= 0);
+      eassert (LENGTH (tree) > 0);
       return;
     }
 
@@ -1430,12 +1431,12 @@
       while (! NULL_LEFT_CHILD (successor))
 	{
 	  successor->total_length += absorb;
-	  eassert (TOTAL_LENGTH (successor) >= 0);
+	  eassert (LENGTH (successor) > 0);
 	  successor = successor->left;
 	}
 
       successor->total_length += absorb;
-      eassert (TOTAL_LENGTH (successor) >= 0);
+      eassert (LENGTH (successor) > 0);
       delete_interval (i);
       return successor;
     }
@@ -1457,7 +1458,7 @@
 
       successor = INTERVAL_PARENT (successor);
       successor->total_length -= absorb;
-      eassert (TOTAL_LENGTH (successor) >= 0);
+      eassert (LENGTH (successor) > 0);
     }
 
   /* This must be the rightmost or last interval and cannot
@@ -1486,12 +1487,12 @@
       while (! NULL_RIGHT_CHILD (predecessor))
 	{
 	  predecessor->total_length += absorb;
-	  eassert (TOTAL_LENGTH (predecessor) >= 0);
+	  eassert (LENGTH (predecessor) > 0);
 	  predecessor = predecessor->right;
 	}
 
       predecessor->total_length += absorb;
-      eassert (TOTAL_LENGTH (predecessor) >= 0);
+      eassert (LENGTH (predecessor) > 0);
       delete_interval (i);
       return predecessor;
     }
@@ -1513,7 +1514,7 @@
 
       predecessor = INTERVAL_PARENT (predecessor);
       predecessor->total_length -= absorb;
-      eassert (TOTAL_LENGTH (predecessor) >= 0);
+      eassert (LENGTH (predecessor) > 0);
     }
 
   /* This must be the leftmost or first interval and cannot
@@ -1528,6 +1529,8 @@
 {
   register INTERVAL target = make_interval ();
 
+  eassert (LENGTH (source) > 0);
+
   target->total_length = source->total_length;
   target->position = source->position;
 
@@ -1538,6 +1541,7 @@
   if (! NULL_RIGHT_CHILD (source))
     set_interval_right (target, reproduce_tree (source->right, target));
 
+  eassert (LENGTH (target) > 0);
   return target;
 }
 
@@ -1766,7 +1770,7 @@
 
   if (! NILP (fallback))
     return fallback;
-  /* Check for alternative properties */
+  /* Check for alternative properties.  */
   tail = Fassq (prop, Vchar_property_alias_alist);
   if (! NILP (tail))
     {
@@ -2434,7 +2438,7 @@
 				 end, end_byte);
     }
 
-  /* Rounding to char boundaries can theoretically ake this interval
+  /* Rounding to char boundaries can theoretically make this interval
      spurious.  If so, delete one child, and copy its property list
      to this interval.  */
   if (LEFT_TOTAL_LENGTH (i) + RIGHT_TOTAL_LENGTH (i) >= TOTAL_LENGTH (i))