Subject: bug#40576: call-process-region does not accept nil as first argument

From: Eli Zaretskii <[email-masked]>
Date: Sun, 12 Apr 2020 19:21:06 +0300

> From: Philipp Stephani <[email-masked]>
> Date: Sun, 12 Apr 2020 18:01:40 +0200
> Cc: [email-masked]
> 
> > According to the documentation of call-process-region,
> >   If START is nil, that means to use the entire buffer contents; END is
> > ignored.
> >
> > But when I run:
> > (call-process-region nil nil "/bin/cat" t (current-buffer))
> > I get:
> > Debugger entered--Lisp error: (wrong-type-argument integer-or-marker-p nil)
> >   call-process-region(nil nil "/bin/cat" t #<buffer *scratch*>)
> >   eval((call-process-region nil nil "/bin/cat" t (current-buffer)) nil)
> >   elisp--eval-last-sexp(t)
> >   eval-last-sexp(t)
> >   eval-print-last-sexp(nil)
> >   funcall-interactively(eval-print-last-sexp nil)
> >   call-interactively(eval-print-last-sexp nil nil)
> >   command-execute(eval-print-last-sexp)
> >
> > If, instead, I run:
> > (call-process-region (point-min) (point-max) "/bin/cat" t (current-buffer))
> >
> > It works.
> >
> > Am I interpreting the documentation wrong?
> 
> Nope, looks like a genuine bug (that happens only if DELETE is non-nil).

Right.  But there's more here than meets the eye, because the change
after which we started advertising the special meaning of nil for
START exposed a problem: write_region, called from create_temp_file,
has special meaning for START = nil: it widens the buffer and writes
the entire buffer contents to the temp file.  Which isn't right when
write_region is called from call-process-region, as it allows access
to inaccessible portion of the buffer, something we shouldn't do.

So I propose the patch below to fix this bug on the master branch.
Any objections?

diff --git a/src/callproc.c b/src/callproc.c
index 8883415..7f495a3 100644
--- a/src/callproc.c
+++ b/src/callproc.c
@@ -1039,8 +1039,8 @@ DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
 
 START and END are normally buffer positions specifying the part of the
 buffer to send to the process.
-If START is nil, that means to use the entire buffer contents; END is
-ignored.
+If START is nil, that means to use the entire accessible part of the
+buffer; END is ignored.
 If START is a string, then send that string to the process
 instead of any buffer contents; END is ignored.
 The remaining arguments are optional.
@@ -1087,6 +1087,14 @@ t (mix it with ordinary output), or a file name string.
       empty_input = XFIXNUM (start) == XFIXNUM (end);
     }
 
+  if (NILP (start))
+    {
+      XSETFASTINT (start, BEGV);
+      args[0] = start;
+      XSETFASTINT (end, ZV);
+      args[1] = end;
+    }
+
   if (!empty_input)
     fd = create_temp_file (nargs, args, &infile);
   else