203 points · by rurban · 6y ago · 98 comments
The fun part was that the way the data got chunked through the pipes was not deterministic, so sometimes you got the desired output from grep, but other times just "binary file matches", even when the raw output from the first tool was identical in both runs. That took quite some head scratching to figure out.
Actually I recently found that coreutils and ls behave fairly well with funny filenames:
Here is an invalid utf-8 byte and then a valid utf-8 sequence
$ x=$'\xce\xce\xbc'
$ touch "$x"
You can list it: $ ls
?μ
And here 'ls' does better than other tools that display filenames. It shows the invalid byte and then keeps decoding with error recovery: $ ls --escape
\316μ
However GNU stat (which I think is also in coreutils) does something similar, but weirdly messed up: $ stat *
File: ''$'\316''μ'
(it looks like it's outputting a valid shell string, except with extra quotes)-----
Most command line tools are not aware of stuff like this. For example you can touch "x$ANSI_TERMINAL_CODES" and if you do "bash x??" or "python x??", then your terminal will change color because of the escape codes printed back to the terminal.
I just changed Oil to use a well-defined format I called QSN (quoted string notation):
http://www.oilshell.org/blog/2020/04/release-0.8.pre4.html#t...
It adapts Rust's string literal syntax to express arbitrary byte strings precisely and losslessly. (JSON can't express arbitrary byte strings.)
The QSN encoder does UTF-8 decoding with a specific error recovery mechanism. So it's basically like what ls and stat do, but it's more precise.
(If anyone is interested in QSN, please contact me. I think it's more generally useful in a lot of places. It's something we already do but it's precise like JSON.)
https://www.gnu.org/software/coreutils/quotes.html
At least with Gnu, you can recompile your own, non-broken version, which is the only saving grace of these stupid, trendy changes.
% touch foo
% touch 'bar baz'
% ls
'bar baz' fooCan you be more specific? I don't see any commmit or release on that date.
I guess you had this commit in mind:
https://git.savannah.gnu.org/cgit/grep.git/commit/?id=cd36ab...
Maybe the fix would be to only activate the "detect binary files" code if stdout isatty?
Because it is a nice feature when I do a big grep to find something among my home directory or the entire filesystem. It is certainly annoying to get binary garbage in my terminal. Or maybe the binary detection could get smarter, maybe making the determination on a match-by-match basis ("This line I'm about to output is a kilobyte and half of it is non-printable", say).
Though, ack-grep doesn't seem to avoid putting binary garbage on my terminal, so maybe reasonable to switch to something that isn't so clever? Most of my terminal greping is done with ack these days, so I'd probably be happy with gnu-grep disabling this cleverness.
IIRC the grep heuristic only considers a short prefix of the file. If the garbage comes later, you lose. Unfortunately, this makes things seem a bit unpredictable.
This was changed about five years ago to just keep looking. Which makes things a bit unpredictable in a different way.
At a glance, I couldn't find a reference on the grep page:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/g...
> The input files shall be text files.
"Text file" is defined in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1... :
> A file that contains characters organized into zero or more lines. The lines do not contain NUL characters and none can exceed {LINE_MAX} bytes in length, including the <newline> character.
Interesting, especially the part about the LINE_MAX. Even though it kinda makes sense, I would never have thought that having a very long line makes a file a non-text file when all characters are 'normal' characters.
And I also think that this would not be a discussion if it was just about NUL. There is something else that grep does not like (escape characters? Invalid utf8? Wrong codepage? I don't know), but this makes the binary detection annoying.
$ printf 'a\222b' | LC_ALL=en_US.UTF-8 grep a
Binary file (standard input) matches
This is POSIX-compliant, because "character" is defined as:> A sequence of one or more bytes representing a single graphic symbol or control code.
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1...
Logs need a lot of attention.
'CWE-117: Improper Output Neutralization for Logs'
That is something probably often forgotten when simply dumping some requests into a log, but at least it should be obvious that the source of the content is untrusted. On the other hand, a log file is a file on your server, so you would probably think of it as nothing dangerous, as everybody has cared about CWE-117, right? ;-)
And I was wondering all the time why mysql reported this strange error "SQL error in Binary file" when the .sql file was clearly a text file...
The speed benefit isn't really a huge factor; working how I'd expect, omitting ignored files, being able to specify file extensions to search, plus simple editor integration. Amazing.
strings file | grep search_pattern
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=c...
edit: I would say that if you are doing forensics on an untrusted binary and you are not using a dedicated VM for it then you are not careful enough. objdump, nm are still attack vectors, not to mention debuggers and disassemblers.
This is a serious anti-feature as far as I can see.. can someone clarify otherwise for me?
Wuff, reminds me of the completely incompatible difference between BSD sed and GNU sed
alias rg="rg --no-heading"
It will still show file paths and line numbers. Those can be disabled too, with `rg --no-heading --no-line-number --no-filename`.Maybe it's just me, but that sounds like a bad default. I can definitely imagine people being confused by that.
You can disable all smart filtering (gitignore, hidden, binary) with `rg -uuu foo`. That will search the same stuff that `grep -r foo ./` will.
GNU grep doesn't have look-arounds either. It does have back-references, but that doesn't impact searches that don't use back-references.
I don't think there is really a concise way to describe why ripgrep is faster when comparing apples-to-apples. It depends on the queries and the corpus. The primary reasons are that it makes more efficient use of the hardware with algorithms that utilize SIMD.
If you do an apples-to-oranges comparison (i.e., "why is `rg foo ./` so much faster than `grep -r foo ./`), then the answer is pretty easily "because ripgrep uses parallelism and employs smart filtering by default."
Why don't most grep utilities just do the obvious right thing and support searches on .c* or .h or .txt or whatever? (HN is mangling the text, but you can read the above as "star dot c star," "star dot h," or "star dot txt.")
To put it another way, do you think you could've written ripgrep in another language? In C? In C++? In Go?