99b86a7
To: vim-dev@vim.org
99b86a7
Subject: About patch 7.1.130
99b86a7
Fcc: outbox
99b86a7
From: Bram Moolenaar <Bram@moolenaar.net>
99b86a7
Mime-Version: 1.0
99b86a7
Content-Type: text/plain; charset=ISO-8859-1
99b86a7
Content-Transfer-Encoding: 8bit
99b86a7
------------
99b86a7
99b86a7
Patch 7.1.130
99b86a7
Problem:    Crash with specific order of undo and redo. (A.Politz)
99b86a7
Solution:   Clear and adjust pointers properly.  Add u_check() for debugging.
99b86a7
Files:	    src/undo.c, src/structs.h
99b86a7
99b86a7
99b86a7
*** ../vim-7.1.129/src/undo.c	Thu May 10 20:01:43 2007
99b86a7
--- src/undo.c	Mon Oct  1 22:49:16 2007
99b86a7
***************
99b86a7
*** 76,81 ****
99b86a7
--- 76,87 ----
99b86a7
   * buffer is unloaded.
99b86a7
   */
99b86a7
  
99b86a7
+ /* Uncomment the next line for including the u_check() function.  This warns
99b86a7
+  * for errors in the debug information. */
99b86a7
+ /* #define U_DEBUG 1 */
99b86a7
+ #define UH_MAGIC 0x18dade	/* value for uh_magic when in use */
99b86a7
+ #define UE_MAGIC 0xabc123	/* value for ue_magic when in use */
99b86a7
+ 
99b86a7
  #include "vim.h"
99b86a7
  
99b86a7
  /* See below: use malloc()/free() for memory management. */
99b86a7
***************
99b86a7
*** 113,118 ****
99b86a7
--- 119,213 ----
99b86a7
   */
99b86a7
  static int	undo_undoes = FALSE;
99b86a7
  
99b86a7
+ #ifdef U_DEBUG
99b86a7
+ /*
99b86a7
+  * Check the undo structures for being valid.  Print a warning when something
99b86a7
+  * looks wrong.
99b86a7
+  */
99b86a7
+ static int seen_b_u_curhead;
99b86a7
+ static int seen_b_u_newhead;
99b86a7
+ static int header_count;
99b86a7
+ 
99b86a7
+     static void
99b86a7
+ u_check_tree(u_header_T *uhp,
99b86a7
+ 	u_header_T *exp_uh_next,
99b86a7
+ 	u_header_T *exp_uh_alt_prev)
99b86a7
+ {
99b86a7
+     u_entry_T *uep;
99b86a7
+ 
99b86a7
+     if (uhp == NULL)
99b86a7
+ 	return;
99b86a7
+     ++header_count;
99b86a7
+     if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
99b86a7
+     {
99b86a7
+ 	EMSG("b_u_curhead found twice (looping?)");
99b86a7
+ 	return;
99b86a7
+     }
99b86a7
+     if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
99b86a7
+     {
99b86a7
+ 	EMSG("b_u_newhead found twice (looping?)");
99b86a7
+ 	return;
99b86a7
+     }
99b86a7
+ 
99b86a7
+     if (uhp->uh_magic != UH_MAGIC)
99b86a7
+ 	EMSG("uh_magic wrong (may be using freed memory)");
99b86a7
+     else
99b86a7
+     {
99b86a7
+ 	/* Check pointers back are correct. */
99b86a7
+ 	if (uhp->uh_next != exp_uh_next)
99b86a7
+ 	{
99b86a7
+ 	    EMSG("uh_next wrong");
99b86a7
+ 	    smsg((char_u *)"expected: 0x%x, actual: 0x%x",
99b86a7
+ 						   exp_uh_next, uhp->uh_next);
99b86a7
+ 	}
99b86a7
+ 	if (uhp->uh_alt_prev != exp_uh_alt_prev)
99b86a7
+ 	{
99b86a7
+ 	    EMSG("uh_alt_prev wrong");
99b86a7
+ 	    smsg((char_u *)"expected: 0x%x, actual: 0x%x",
99b86a7
+ 					   exp_uh_alt_prev, uhp->uh_alt_prev);
99b86a7
+ 	}
99b86a7
+ 
99b86a7
+ 	/* Check the undo tree at this header. */
99b86a7
+ 	for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
99b86a7
+ 	{
99b86a7
+ 	    if (uep->ue_magic != UE_MAGIC)
99b86a7
+ 	    {
99b86a7
+ 		EMSG("ue_magic wrong (may be using freed memory)");
99b86a7
+ 		break;
99b86a7
+ 	    }
99b86a7
+ 	}
99b86a7
+ 
99b86a7
+ 	/* Check the next alt tree. */
99b86a7
+ 	u_check_tree(uhp->uh_alt_next, uhp->uh_next, uhp);
99b86a7
+ 
99b86a7
+ 	/* Check the next header in this branch. */
99b86a7
+ 	u_check_tree(uhp->uh_prev, uhp, NULL);
99b86a7
+     }
99b86a7
+ }
99b86a7
+ 
99b86a7
+     void
99b86a7
+ u_check(int newhead_may_be_NULL)
99b86a7
+ {
99b86a7
+     seen_b_u_newhead = 0;
99b86a7
+     seen_b_u_curhead = 0;
99b86a7
+     header_count = 0;
99b86a7
+ 
99b86a7
+     u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
99b86a7
+ 
99b86a7
+     if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
99b86a7
+ 	    && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
99b86a7
+ 	EMSGN("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
99b86a7
+     if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
99b86a7
+ 	EMSGN("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
99b86a7
+     if (header_count != curbuf->b_u_numhead)
99b86a7
+     {
99b86a7
+ 	EMSG("b_u_numhead invalid");
99b86a7
+ 	smsg((char_u *)"expected: %ld, actual: %ld",
99b86a7
+ 			       (long)header_count, (long)curbuf->b_u_numhead);
99b86a7
+     }
99b86a7
+ }
99b86a7
+ #endif
99b86a7
+ 
99b86a7
  /*
99b86a7
   * Save the current line for both the "u" and "U" command.
99b86a7
   * Returns OK or FAIL.
99b86a7
***************
99b86a7
*** 243,248 ****
99b86a7
--- 338,346 ----
99b86a7
      if (!undo_allowed())
99b86a7
  	return FAIL;
99b86a7
  
99b86a7
+ #ifdef U_DEBUG
99b86a7
+     u_check(FALSE);
99b86a7
+ #endif
99b86a7
  #ifdef FEAT_NETBEANS_INTG
99b86a7
      /*
99b86a7
       * Netbeans defines areas that cannot be modified.  Bail out here when
99b86a7
***************
99b86a7
*** 294,299 ****
99b86a7
--- 392,400 ----
99b86a7
  	    uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T));
99b86a7
  	    if (uhp == NULL)
99b86a7
  		goto nomem;
99b86a7
+ #ifdef U_DEBUG
99b86a7
+ 	    uhp->uh_magic = UH_MAGIC;
99b86a7
+ #endif
99b86a7
  	}
99b86a7
  	else
99b86a7
  	    uhp = NULL;
99b86a7
***************
99b86a7
*** 316,323 ****
99b86a7
  	{
99b86a7
  	    u_header_T	    *uhfree = curbuf->b_u_oldhead;
99b86a7
  
99b86a7
! 	    /* If there is no branch only free one header. */
99b86a7
! 	    if (uhfree->uh_alt_next == NULL)
99b86a7
  		u_freeheader(curbuf, uhfree, &old_curhead);
99b86a7
  	    else
99b86a7
  	    {
99b86a7
--- 417,427 ----
99b86a7
  	{
99b86a7
  	    u_header_T	    *uhfree = curbuf->b_u_oldhead;
99b86a7
  
99b86a7
! 	    if (uhfree == old_curhead)
99b86a7
! 		/* Can't reconnect the branch, delete all of it. */
99b86a7
! 		u_freebranch(curbuf, uhfree, &old_curhead);
99b86a7
! 	    else if (uhfree->uh_alt_next == NULL)
99b86a7
! 		/* There is no branch, only free one header. */
99b86a7
  		u_freeheader(curbuf, uhfree, &old_curhead);
99b86a7
  	    else
99b86a7
  	    {
99b86a7
***************
99b86a7
*** 326,331 ****
99b86a7
--- 430,438 ----
99b86a7
  		    uhfree = uhfree->uh_alt_next;
99b86a7
  		u_freebranch(curbuf, uhfree, &old_curhead);
99b86a7
  	    }
99b86a7
+ #ifdef U_DEBUG
99b86a7
+ 	    u_check(TRUE);
99b86a7
+ #endif
99b86a7
  	}
99b86a7
  
99b86a7
  	if (uhp == NULL)		/* no undo at all */
99b86a7
***************
99b86a7
*** 478,483 ****
99b86a7
--- 585,593 ----
99b86a7
      uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T));
99b86a7
      if (uep == NULL)
99b86a7
  	goto nomem;
99b86a7
+ #ifdef U_DEBUG
99b86a7
+     uep->ue_magic = UE_MAGIC;
99b86a7
+ #endif
99b86a7
  
99b86a7
      uep->ue_size = size;
99b86a7
      uep->ue_top = top;
99b86a7
***************
99b86a7
*** 525,530 ****
99b86a7
--- 635,643 ----
99b86a7
      curbuf->b_u_synced = FALSE;
99b86a7
      undo_undoes = FALSE;
99b86a7
  
99b86a7
+ #ifdef U_DEBUG
99b86a7
+     u_check(FALSE);
99b86a7
+ #endif
99b86a7
      return OK;
99b86a7
  
99b86a7
  nomem:
99b86a7
***************
99b86a7
*** 955,960 ****
99b86a7
--- 1068,1076 ----
99b86a7
      int		empty_buffer;		    /* buffer became empty */
99b86a7
      u_header_T	*curhead = curbuf->b_u_curhead;
99b86a7
  
99b86a7
+ #ifdef U_DEBUG
99b86a7
+     u_check(FALSE);
99b86a7
+ #endif
99b86a7
      old_flags = curhead->uh_flags;
99b86a7
      new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
99b86a7
  	       ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
99b86a7
***************
99b86a7
*** 1186,1191 ****
99b86a7
--- 1302,1310 ----
99b86a7
      /* The timestamp can be the same for multiple changes, just use the one of
99b86a7
       * the undone/redone change. */
99b86a7
      curbuf->b_u_seq_time = curhead->uh_time;
99b86a7
+ #ifdef U_DEBUG
99b86a7
+     u_check(FALSE);
99b86a7
+ #endif
99b86a7
  }
99b86a7
  
99b86a7
  /*
99b86a7
***************
99b86a7
*** 1515,1521 ****
99b86a7
  }
99b86a7
  
99b86a7
  /*
99b86a7
!  * Free one header and its entry list and adjust the pointers.
99b86a7
   */
99b86a7
      static void
99b86a7
  u_freeheader(buf, uhp, uhpp)
99b86a7
--- 1634,1640 ----
99b86a7
  }
99b86a7
  
99b86a7
  /*
99b86a7
!  * Free one header "uhp" and its entry list and adjust the pointers.
99b86a7
   */
99b86a7
      static void
99b86a7
  u_freeheader(buf, uhp, uhpp)
99b86a7
***************
99b86a7
*** 1523,1528 ****
99b86a7
--- 1642,1649 ----
99b86a7
      u_header_T	    *uhp;
99b86a7
      u_header_T	    **uhpp;	/* if not NULL reset when freeing this header */
99b86a7
  {
99b86a7
+     u_header_T	    *uhap;
99b86a7
+ 
99b86a7
      /* When there is an alternate redo list free that branch completely,
99b86a7
       * because we can never go there. */
99b86a7
      if (uhp->uh_alt_next != NULL)
99b86a7
***************
99b86a7
*** 1540,1546 ****
99b86a7
      if (uhp->uh_prev == NULL)
99b86a7
  	buf->b_u_newhead = uhp->uh_next;
99b86a7
      else
99b86a7
! 	uhp->uh_prev->uh_next = uhp->uh_next;
99b86a7
  
99b86a7
      u_freeentries(buf, uhp, uhpp);
99b86a7
  }
99b86a7
--- 1661,1668 ----
99b86a7
      if (uhp->uh_prev == NULL)
99b86a7
  	buf->b_u_newhead = uhp->uh_next;
99b86a7
      else
99b86a7
! 	for (uhap = uhp->uh_prev; uhap != NULL; uhap = uhap->uh_alt_next)
99b86a7
! 	    uhap->uh_next = uhp->uh_next;
99b86a7
  
99b86a7
      u_freeentries(buf, uhp, uhpp);
99b86a7
  }
99b86a7
***************
99b86a7
*** 1585,1590 ****
99b86a7
--- 1707,1714 ----
99b86a7
      /* Check for pointers to the header that become invalid now. */
99b86a7
      if (buf->b_u_curhead == uhp)
99b86a7
  	buf->b_u_curhead = NULL;
99b86a7
+     if (buf->b_u_newhead == uhp)
99b86a7
+ 	buf->b_u_newhead = NULL;  /* freeing the newest entry */
99b86a7
      if (uhpp != NULL && uhp == *uhpp)
99b86a7
  	*uhpp = NULL;
99b86a7
  
99b86a7
***************
99b86a7
*** 1594,1599 ****
99b86a7
--- 1718,1726 ----
99b86a7
  	u_freeentry(uep, uep->ue_size);
99b86a7
      }
99b86a7
  
99b86a7
+ #ifdef U_DEBUG
99b86a7
+     uhp->uh_magic = 0;
99b86a7
+ #endif
99b86a7
      U_FREE_LINE((char_u *)uhp);
99b86a7
      --buf->b_u_numhead;
99b86a7
  }
99b86a7
***************
99b86a7
*** 1609,1614 ****
99b86a7
--- 1736,1744 ----
99b86a7
      while (n > 0)
99b86a7
  	U_FREE_LINE(uep->ue_array[--n]);
99b86a7
      U_FREE_LINE((char_u *)uep->ue_array);
99b86a7
+ #ifdef U_DEBUG
99b86a7
+     uep->ue_magic = 0;
99b86a7
+ #endif
99b86a7
      U_FREE_LINE((char_u *)uep);
99b86a7
  }
99b86a7
  
99b86a7
*** ../vim-7.1.129/src/structs.h	Sun Aug 12 15:50:26 2007
99b86a7
--- src/structs.h	Sat Sep 29 15:03:38 2007
99b86a7
***************
99b86a7
*** 278,283 ****
99b86a7
--- 278,286 ----
99b86a7
      linenr_T	ue_lcount;	/* linecount when u_save called */
99b86a7
      char_u	**ue_array;	/* array of lines in undo block */
99b86a7
      long	ue_size;	/* number of lines in ue_array */
99b86a7
+ #ifdef U_DEBUG
99b86a7
+     int		ue_magic;	/* magic number to check allocation */
99b86a7
+ #endif
99b86a7
  };
99b86a7
  
99b86a7
  struct u_header
99b86a7
***************
99b86a7
*** 300,305 ****
99b86a7
--- 303,311 ----
99b86a7
      visualinfo_T uh_visual;	/* Visual areas before undo/after redo */
99b86a7
  #endif
99b86a7
      time_t	uh_time;	/* timestamp when the change was made */
99b86a7
+ #ifdef U_DEBUG
99b86a7
+     int		uh_magic;	/* magic number to check allocation */
99b86a7
+ #endif
99b86a7
  };
99b86a7
  
99b86a7
  /* values for uh_flags */
99b86a7
*** ../vim-7.1.129/src/version.c	Mon Oct  1 20:33:45 2007
99b86a7
--- src/version.c	Mon Oct  1 22:50:23 2007
99b86a7
***************
99b86a7
*** 668,669 ****
99b86a7
--- 668,671 ----
99b86a7
  {   /* Add new patch number below this line */
99b86a7
+ /**/
99b86a7
+     130,
99b86a7
  /**/
99b86a7
99b86a7
-- 
99b86a7
FIRST SOLDIER:  So they wouldn't be able to bring a coconut back anyway.
99b86a7
SECOND SOLDIER: Wait a minute! Suppose two swallows carried it together?
99b86a7
FIRST SOLDIER:  No, they'd have to have it on a line.
99b86a7
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
99b86a7
99b86a7
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
99b86a7
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
99b86a7
\\\        download, build and distribute -- http://www.A-A-P.org        ///
99b86a7
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///