Subject: Re: 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 08:54:55 +0300

> From: Sam Steingold <[email-masked]>
> Date: Fri, 06 Oct 2023 13:11:35 -0400
> 
> 
> emacs -Q
> 
> (insert-file-contents "/dev/null")
> ==>
> Debugger entered--Lisp error: (error "Maximum buffer size exceeded")
>   insert-file-contents("/dev/null")

This is a "feature".  From the doc string:

  When inserting data from a special file (e.g., /dev/urandom), you
  can’t specify VISIT or BEG, and END should be specified to avoid
  inserting unlimited data into the buffer from some special files
  which otherwise could supply infinite amounts of data.

Maybe we could make a special exception for the null-device, whose
name we generally know on each supported system.

Technically, the problem is that the null device is seekable, so this:

  if (seekable || !NILP (end))
    total = end_offset - beg_offset;

computes 'total' to be a very large value, and then this:

  /* Ensure the gap is at least one byte larger than needed for the
     estimated file size, so that in the usual case we read to EOF
     without reallocating.  */
  if (GAP_SIZE <= total)
    make_gap (total - GAP_SIZE + 1);

attempts to make a gap very large, which errors out.  And that is
before we try to read even a single byte from the file.

Po Lu and Paul, any ideas?