Subject: grep seems to write some error messages to stdout data stream --- shouldn't it be to stderr?

From: "John Refling" <[email-masked]>
Date: Mon, 16 May 2016 23:03:01 -0700

I noticed this when I had an issue with 'binary file detection' discussed
in bug report #22838.

In 2.24 when the binary file detection triggered, not only was some of my
output data missing (since processing stopped), there was no error message
since grep sent that error message to stdout, along with part of my data.  I
was piping the output to another process.

I just built latest 2.25, and the binary file detection is improved, but the
error message STILL is hidden, since it is sent to redirected stdout, along
with data.  Verified by building 2.25 on Cygwin and FreeBSD.

A contrived example:

server-grep-2.25> ./src/grep.exe . src/grep.exe > x

server-grep-2.25> cat x

Binary file src/grep.exe matches

IMHO, error messages should NEVER be injected into the same stream as the
users input / output data, firstly because it corrupts the data, and
secondly if the output is redirected, the user will never see the error
message.

Not sure if this affects all error messages, or just the 'Binary file FOO
matches' message.

Relevant areas of src/grep.c are:

printf_errno (_("Binary file %s matches\n"), filename);

printf_errno (char const *format, ...)
{
  va_list ap;
  va_start (ap, format);
  if (vfprintf (stdout, format, ap) < 0)      // should be stderr IMHO
    stdout_errno = errno;
  va_end (ap);
}

Seems to me that grep should NEVER add more data to the output stream than
was in the input stream (by adding anything, including error messages).

Just seems wrong.

Shouldn't there be exactly the same or less data, and not new data in the
output that was not even part of the original input data?

My 2 cents,

John Refling