Subject: bug#66375: 30.0.50; (error "Maximum buffer size exceeded") from (insert-file-contents "/dev/null")

From: Eli Zaretskii <[email-masked]>
Date: Sat, 07 Oct 2023 10:43:24 +0300

> From: Po Lu <[email-masked]>
> Cc: [email-masked],  Paul Eggert <[email-masked]>,  [email-masked]
> Date: Sat, 07 Oct 2023 14:43:45 +0800
> 
> --- a/src/fileio.c
> +++ b/src/fileio.c
> @@ -4746,7 +4746,7 @@ DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents,
>        goto handled;
>      }
>  
> -  if (seekable || !NILP (end))
> +  if (((regular || !NILP (end)) && seekable) || !NILP (end))
>      total = end_offset - beg_offset;
>    else
>      /* For a special file, all we can do is guess.  */
> 

This does a couple of redundant tests:

  . regular non-zero means seekable must be non-zero
  . NILP (end) is tested twice for now good reason

I think the above should be cleaned up to better understand the logic.

More generally, I think 'total' should not be computed in this naïve
way when end_offset is -1, i.e. unless end_offset is either the result
of fstat/stat or the result of actually seeking to the end of
file/device.