Subject: bug#16433: Test case for newline cache corruption

From: Eli Zaretskii <[email-masked]>
Date: Mon, 26 May 2014 22:28:22 +0300

> From: Ken Olum <[email-masked]>
> Cc: [email-masked]
> Date: Mon, 26 May 2014 11:29:22 -0400
> 
>    Also, is your build optimized or not?  More generally, what are the
>    values of system-configuration and system-configuration-options in the
>    build where you reproduce this?  If your build options are different
>    from mine, especially the optimization switches, perhaps that is part
>    of the issue somehow.
> 
> All I did was to download the trunk distribution and say "make".

This means the build is optimized.  This might get in the way when we
will need to use GDB, but for now it's OK.

Below please find a simple patch that adds instrumentation to the
cache operations.  The trace goes to the *Messages* buffer.  Please
apply the patch, then run your recipe, and show the trace for it.  (It
is best to mark the place in *Messages* where you actually start and
end the recipe with some prominent string, because the newline cache
is consulted _a_lot_.)

Thanks.

=== modified file 'src/region-cache.c'
--- a/src/region-cache.c	2014-05-03 08:24:51 +0000
+++ b/src/region-cache.c	2014-05-26 19:26:53 +0000
@@ -459,6 +459,15 @@
 invalidate_region_cache (struct buffer *buf, struct region_cache *c,
 			 ptrdiff_t head, ptrdiff_t tail)
 {
+  char buff[1024];
+
+  if (c == buf->newline_cache)
+    {
+      sprintf (buff, "invalidate_region_cache (%s, %d, %d, %d)",
+	       SSDATA (BVAR (buf, name)),
+	       BUF_BEG (buf) + head, BUF_Z (buf) - tail, BUF_Z (buf));
+      message_dolog (buff, strlen (buff), false, true);
+    }
   /* Let chead = c->beg_unchanged, and
          ctail = c->end_unchanged.
      If z-tail < beg+chead by a large amount, or
@@ -474,8 +483,14 @@
        > PRESERVE_THRESHOLD)
       || ((BUF_BEG (buf) + head) - (BUF_Z (buf) - c->end_unchanged)
           > PRESERVE_THRESHOLD))
-    revalidate_region_cache (buf, c);
-
+    {
+      revalidate_region_cache (buf, c);
+      if (c == buf->newline_cache)
+	message_dolog (" (revalidated)\n", sizeof (" (revalidated)\n") - 1,
+		       false, true);
+    }
+  else if (c == buf->newline_cache)
+    message_dolog ("\n", 1, false, true);
 
   if (head < c->beg_unchanged)
     c->beg_unchanged = head;
@@ -666,6 +681,15 @@
 know_region_cache (struct buffer *buf, struct region_cache *c,
 		   ptrdiff_t start, ptrdiff_t end)
 {
+  char buff[1024];
+
+  if (c == buf->newline_cache)
+    {
+      sprintf (buff, "know_region_cache (%s, %d, %d, %d)\n",
+	       SSDATA (BVAR (buf, name)), start, end, BUF_Z (buf));
+      message_dolog (buff, strlen (buff), false, true);
+    }
+
   revalidate_region_cache (buf, c);
 
   set_cache_region (c, start, end, 1);