]> git.eshelyaron.com Git - emacs.git/commitdiff
Refactor insert-file-contents overlap check
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 20 Jul 2025 22:20:31 +0000 (15:20 -0700)
committerEshel Yaron <me@eshelyaron.com>
Fri, 25 Jul 2025 08:11:51 +0000 (10:11 +0200)
* src/fileio.c (Finsert_file_contents):
Simplify and clarify file overlap check.

(cherry picked from commit e879533f4b9cd12a6847f4d69c866c29d5923541)

src/fileio.c

index 3f41e70e66d9be83df8df63deacc77ccae4bf678..2c60405ef35e2962fe04ffa2a601722524d08a27 100644 (file)
@@ -4501,7 +4501,6 @@ by calling `format-decode', which see.  */)
          if (bufpos != nread)
            break;
        }
-      off_t same_at_start_pos = beg_offset + (same_at_start - BEGV_BYTE);
 
       /* Find the end position, which is end_offset if given,
         the file's end otherwise.  */
@@ -4530,11 +4529,8 @@ by calling `format-decode', which see.  */)
                    {
                      /* Shrink the file's head if the file shrank to
                         be smaller than its head.  */
-                     if (endpos < same_at_start_pos)
-                       {
-                         same_at_start_pos = endpos;
-                         same_at_start = endpos - beg_offset + BEGV_BYTE;
-                       }
+                     if (endpos - beg_offset < same_at_start - BEGV_BYTE)
+                       same_at_start = endpos - beg_offset + BEGV_BYTE;
                    }
                }
            }
@@ -4550,10 +4546,11 @@ by calling `format-decode', which see.  */)
          /* How much can we scan in the next step?  Compare with poslim
             to prevent overlap of the matching head with the matching tail.
             The 'same_at_start_pos' limit prevents overlap in the buffer's
-            head and tail, and the 'endpos - (same_at_end - same_at_start)'
-            limit prevents overlap in the inserted file's head and tail.  */
-         off_t poslim = max (same_at_start_pos,
-                             endpos - (same_at_end - same_at_start));
+            head and tail, and the 'file_overlap_pos' limit prevents
+            overlap in the inserted file's head and tail.  */
+         off_t same_at_start_pos = beg_offset + (same_at_start - BEGV_BYTE);
+         off_t file_overlap_pos = endpos - (same_at_end - same_at_start);
+         off_t poslim = max (same_at_start_pos, file_overlap_pos);
          /* Do not scan more than sizeof read_buf at a time, and stop
             the scan if it can go no more.  */
          trial = min (curpos - poslim, sizeof read_buf);