public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: cygwin@cygwin.com, ynnor@mm.st
Subject: Re: Bug: grep behaves incorrectly under the locale C.UTF-8, if a file contains Umlaut characters
Date: Wed, 24 May 2017 12:33:00 -0000	[thread overview]
Message-ID: <3c344ecb-6ef3-9d54-a627-4714382d4d84@redhat.com> (raw)
In-Reply-To: <1495612367.2760331.986814392.79C77EB2@webmail.messagingengine.com>


[-- Attachment #1.1: Type: text/plain, Size: 3108 bytes --]

On 05/24/2017 02:52 AM, Ronald Fischer wrote:
> I have a file X which contains ASCII text, but also in some lines German
> umlaut characters. The file is classified as:
> 
>      $ file X
>      X: ISO-8859 text, with CRLF line terminators

In ISO-8859, a German umlaut occupies one byte with the high-bit set.

> 
> However, if LANG is set to C.UTF-8, two things happen:
> 
> - grep classifies the file as binary file and produces the error message
> "Binary file X matches" 

In UTF-8, any one-byte sequence with the high bit set in isolation is an
encoding error (all high-bit bytes in UTF-8 occur in 2-or-more byte
sequences).  According to POSIX, grep is only required to operate on
text files, and the definition of a text file includes a requirement
that ALL bytes in the file form valid encodings of characters in the
current locale.  Yes, this means that there are files that are valid
text files in some locales and invalid in others (such as your file
here).  Once you violate the POSIX constraint of passing a non-text file
to grep, all bets are off, and grep can do whatever it wants, including
telling you that a binary file matches.

> 
> - Both the grepped lines (i.e. in our example the non-empty lines) AND
> the error message end up in the standard output (i.e. in file Y).

Yes, that's the current intended behavior in upstream grep. It's not
unique to Cygwin, so complaining here won't change it.

> 
> IMO, there are several problems with this:
> 
> 1. It's hard to see, why an umlaut character makes the file X binary
> under encoding C.UTF-8, 

Because it's not a valid UTF-8 encoding. Use iconv to convert your file
from ISO-8859 to UTF-8 if you want to grep it under C.UTF-8.

> but not under encoding UTF-8 or C.en_EN

Those aren't valid locale names.

But if you mean that it does what you want under LC_ALL=C, that's
because in the straight C locale, there are no multi-byte characters,
and therefore no encoding errors are possible, and therefore you can't
get a binary file in that locale due merely to an encoding error.

> 
> 2. If grep classifies a file as binary, I think the desired behaviour
> would be to NOT produce any output, unless the -a flag has been
> supplied.

Once behavior is in the realm of the undefined, it's hard to say what
the desired behavior should be. But again, if you want the current
behavior changed, it's an upstream issue to complain about on bug-grep,
and not something that I'm going to change for Cygwin in isolation.

> 
> 3. If grep writes a message "Binary file ... matches", this message
> should go to stderr, not stdout. The stdout is supposed to contain only
> a subset of the input lines.

The message "Binary file ... matches" has always gone to stdout, even
before upstream was tightened to flag more encoding errors as binary
files.  Whether the behavior of mixing it with actual output is
desirable is a question for upstream.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

      parent reply	other threads:[~2017-05-24 12:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-24  8:30 Ronald Fischer
2017-05-24 10:05 ` Andrey Repin
2017-05-24 10:20   ` Ronald Fischer
2017-05-24 11:42 ` Erik Bray
2017-05-24 12:33 ` Eric Blake [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3c344ecb-6ef3-9d54-a627-4714382d4d84@redhat.com \
    --to=eblake@redhat.com \
    --cc=cygwin@cygwin.com \
    --cc=ynnor@mm.st \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).