Subject: Re: 31.0.50; Recent change breaks `insert-file-contents-literally'

From: Paul Eggert <[email-masked]>
Date: Wed, 16 Jul 2025 11:47:30 -0700

On 2025-07-16 06:13, Zhengyi Fu wrote:
> the following code evaluates to a string of 16384
> bytes!
> 
> (with-temp-buffer
>    (insert-file-contents-literally "/dev/urandom" nil nil 21)
>    (buffer-string))

Thanks for reporting that. I installed the attached patch.

From e450a7802c544b7fa14fe064c62de1f9b7098e81 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Wed, 16 Jul 2025 11:38:20 -0700
Subject: [PATCH] insert-file-contents small gap bug

Problem reported by Zhengyi Fu (Bug#79031).
* src/fileio.c (Finsert_file_contents): Do not read more than
requested, even when the gap is so small that we use a local buffer.
---
 src/fileio.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/fileio.c b/src/fileio.c
index 2c8855dba91..e33ac4afabf 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -4899,8 +4899,9 @@ DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents,
 	else
 	  {
 	    buf = (char *) BEG_ADDR + PT_BYTE - BEG_BYTE + inserted;
-	    bufsize = min (min (gap_size, total - inserted), IO_BUFSIZE);
+	    bufsize = min (gap_size, IO_BUFSIZE);
 	  }
+	bufsize = min (bufsize, total - inserted);
 
 	if (!seekable && end_offset == TYPE_MAXIMUM (off_t))
 	  {
-- 
2.48.1