From 5284af27ee5250c631ff4ee2f3d8682f0c5df8bc Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 12 Feb 2023 17:30:46 -0800 Subject: [PATCH 1/5] Improve insert-file-contents checking * src/fileio.c (Finsert_file_contents): Check BEG, END, REPLACE for validity before launching into opening files etc. --- src/fileio.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/fileio.c b/src/fileio.c index c672e0f7baf..64337abdaef 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -3908,7 +3908,6 @@ because (1) it preserves some marker positions (in unchanged portions int fd; ptrdiff_t inserted = 0; ptrdiff_t how_much; - off_t beg_offset, end_offset; int unprocessed; specpdl_ref count = SPECPDL_INDEX (); Lisp_Object handler, val, insval, orig_filename, old_undo; @@ -3970,6 +3969,17 @@ because (1) it preserves some marker positions (in unchanged portions goto handled; } + if (!NILP (visit)) + { + if (!NILP (beg) || !NILP (end)) + error ("Attempt to visit less than an entire file"); + if (BEG < Z && NILP (replace)) + error ("Cannot do file visiting in a non-empty buffer"); + } + + off_t beg_offset = !NILP (beg) ? file_offset (beg) : 0; + off_t end_offset = !NILP (end) ? file_offset (end) : -1; + orig_filename = filename; filename = ENCODE_FILE (filename); @@ -4030,22 +4040,7 @@ because (1) it preserves some marker positions (in unchanged portions build_string ("not a regular file"), orig_filename); } - if (!NILP (visit)) - { - if (!NILP (beg) || !NILP (end)) - error ("Attempt to visit less than an entire file"); - if (BEG < Z && NILP (replace)) - error ("Cannot do file visiting in a non-empty buffer"); - } - - if (!NILP (beg)) - beg_offset = file_offset (beg); - else - beg_offset = 0; - - if (!NILP (end)) - end_offset = file_offset (end); - else + if (end_offset < 0) { if (!regular) end_offset = TYPE_MAXIMUM (off_t); -- 2.37.2