]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix insert-file-contents overlap calculation
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 13 Jul 2025 00:03:29 +0000 (17:03 -0700)
committerEshel Yaron <me@eshelyaron.com>
Thu, 24 Jul 2025 08:48:53 +0000 (10:48 +0200)
* src/fileio.c (Finsert_file_contents): Overlap is impossible, so
omit incorrect overflow adjustments that caused later index
calculations to go off the rails (Bug#77315).  Change a ‘ZV_BYTE’
to ‘same_at_end’ for clarity; they have the same value here.

(cherry picked from commit 61a8ce0280668980a4452b112103c9bc879e1f49)

src/fileio.c

index ae033797aa3055448303e7689f1c990332952ba3..d7dd03db17e1f0ba68578bd238f325b2aeb1f3d4 100644 (file)
@@ -4407,7 +4407,6 @@ by calling `format-decode', which see.  */)
       && (NILP (coding_system)
          || ! CODING_REQUIRE_DECODING (&coding)))
     {
-      ptrdiff_t overlap;
       /* There is still a possibility we will find the need to do code
         conversion.  If that happens, set this variable to
         give up on handling REPLACE in the optimized way.  */
@@ -4462,7 +4461,7 @@ by calling `format-decode', which see.  */)
            }
 
          int bufpos = 0;
-         while (bufpos < nread && same_at_start < ZV_BYTE
+         while (bufpos < nread && same_at_start < same_at_end
                 && FETCH_BYTE (same_at_start) == read_buf[bufpos])
            same_at_start++, bufpos++;
          /* If we found a discrepancy, stop the scan.
@@ -4579,12 +4578,6 @@ by calling `format-decode', which see.  */)
                   && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end)))
              same_at_end++;
 
-         /* Don't try to reuse the same piece of text twice.  */
-         overlap = (same_at_start - BEGV_BYTE
-                    - (same_at_end - ZV_BYTE
-                       + endpos));
-         if (overlap > 0)
-           same_at_end += overlap;
          same_at_end_charpos = BYTE_TO_CHAR (same_at_end);
 
          /* Arrange to read only the nonmatching middle part of the file.  */
@@ -4634,7 +4627,6 @@ by calling `format-decode', which see.  */)
     {
       ptrdiff_t same_at_start_charpos;
       ptrdiff_t inserted_chars;
-      ptrdiff_t overlap;
       ptrdiff_t bufpos;
       unsigned char *decoded;
       ptrdiff_t temp;
@@ -4749,10 +4741,6 @@ by calling `format-decode', which see.  */)
               && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end)))
          same_at_end++;
 
-      /* Don't try to reuse the same piece of text twice.  */
-      overlap = same_at_start - BEGV_BYTE - (same_at_end + inserted - ZV_BYTE);
-      if (overlap > 0)
-       same_at_end += overlap;
       same_at_end_charpos = BYTE_TO_CHAR (same_at_end);
 
       /* If display currently starts at beginning of line,