Macos OSX上的grep-f产生segfault

Macos OSX上的grep-f产生segfault,macos,grep,segmentation-fault,freebsd,bsd,Macos,Grep,Segmentation Fault,Freebsd,Bsd,如果您有Mac电脑,请尝试以下方法: echo'abcd*”>grepfile 回显“abc$”>>grepfile 回显“^abc”>>grepfile echo“fojeiwuroiuwet\nljfajsljkfabcddjlfkajlkj\nabcaa\nzzabc\n”| grep-f grepfile 以下是版本: $ grep --v grep (BSD grep) 2.5.1-FreeBSD $ grep --v grep (BSD grep) 2.5.1-FreeBSD $

如果您有Mac电脑,请尝试以下方法:

echo'abcd*”>grepfile
回显“abc$”>>grepfile
回显“^abc”>>grepfile
echo“fojeiwuroiuwet\nljfajsljkfabcddjlfkajlkj\nabcaa\nzzabc\n”| grep-f grepfile
以下是版本:

$ grep --v
grep (BSD grep) 2.5.1-FreeBSD
$ grep --v
grep (BSD grep) 2.5.1-FreeBSD
$ shasum /usr/bin/grep
350ee11e1868e18c9707ea7035184a114f40badf  /usr/bin/grep
$ codesign -dvvv /usr/bin/grep
Executable=/usr/bin/grep
Identifier=com.apple.zgrep
Format=Mach-O thin (x86_64)
CodeDirectory v=20100 size=224 flags=0x0(none) hashes=6+2 location=embedded
Hash type=sha1 size=20
CDHash=1537b3ed49878d5d18482859a37318164a2a40f1
Signature size=4064
Authority=Software Signing
Authority=Apple Code Signing Certification Authority
Authority=Apple Root CA
Info.plist=not bound
Sealed Resources=none
Internal requirements count=2 size=176
这是一台能跟上苹果软件更新的机器(2012年版的rMBP),所以我选择了10.8.4

我验证了从源代码编译的GNU
grep
没有遇到这个问题。事实上,它是2.14版,比2.5.1版多了很多

但是,如果没有一些效率极低的循环为每个正则表达式派生一个grep,那么如何实现针对一系列正则表达式测试某些输入的任务呢

编辑:我采取的解决方法类似于:
读REGEX时;执行[…=~$REGEX]]。。。完成

问题:这个版本的grep是否存在已知的bug?我们如何设置我们的系统,使它们能够与grep的regex文件正常工作

更新:看起来有些人报告说它工作得很好(即使使用这个特定的FreeBSD2.5.1 grep)。我可以采取哪些步骤来找出它可能使用的.so/.dylib?有人能做一个
shasum/usr/bin/grep
并告诉我它是否适合你吗?(我不确定这是否能提供很多信息,但我想知道的是我的计算机配置是否出错,或者这是否是该版本软件存在的实际问题。)

以下是更多信息:

$ codesign -dvvv /usr/bin/grep
Executable=/usr/bin/grep
Identifier=com.apple.zgrep
Format=Mach-O thin (x86_64)
CodeDirectory v=20100 size=224 flags=0x0(none) hashes=6+2 location=embedded
Hash type=sha1 size=20
CDHash=93b823c000188f8737653d8333c90a6db9361d70
Signature size=4064
Authority=Software Signing
Authority=Apple Code Signing Certification Authority
Authority=Apple Root CA
Info.plist=not bound
Sealed Resources=none
Internal requirements count=2 size=208
进一步调查:

$ gdb /usr/bin/grep
GNU gdb 6.3.50-20050815 (Apple version gdb-1824) (Thu Nov 15 10:42:43 UTC 2012)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .... done

(gdb) start -f grepfile
Function "main" not defined.
Make breakpoint pending on future shared library load? (y or [n])
Starting program: /usr/bin/grep -f grepfile
Reading symbols for shared libraries +++.............................. done
abc
abc

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000101000000
0x00007fff89b5d1b3 in memchr ()
(gdb) where
#0  0x00007fff89b5d1b3 in memchr ()
#1  0x00007fff89b8e45a in __sfvwrite ()
#2  0x00007fff89b8e861 in fwrite ()
#3  0x0000000100003138 in _mh_execute_header ()
#4  0x0000000100002988 in _mh_execute_header ()
#5  0x0000000100001c28 in _mh_execute_header ()
#6  0x00007fff8e2d57e1 in start ()
(gdb)

我也重新启动了机器。它在gdb中重复地做同样的事情

我在MacBook Air上安装了OSX 10.8.4,您的示例在默认情况下不会崩溃,但只有在添加
--color
参数时才会崩溃

解释

这种崩溃通常发生在您将wilcard(星号)与终端颜色混合时,这就是软件错误

另请检查另一个更简单的示例:

echo 'abc*' | grep --color=auto -e ".*" -e a
在这里,似乎
--color=auto
起了作用(如果没有它或设置为
never
,它就不会崩溃)。 所以我假设您的grep默认使用终端中的颜色

解决方案

确保您的grep不是已启用颜色的grep的别名,或者默认情况下未启用颜色

您始终可以尝试使用
--color=never
运行grep

对于永久解决方案,我报告了错误报告:

因此,该问题可以在软件中自行解决


如果您想访问更详细的崩溃日志,请转到控制台,显示日志列表并在“用户诊断报告”下找到崩溃日志

例如:


下面是关于这次事故的一些更详细的解释:

A quick test

  $ env -i bsdgrep -Fi without_nls usr.bin/grep/grep.c
  $ env -i gnugrep -Fi without_nls usr.bin/grep/grep.c
  #ifndef WITHOUT_NLS
  #ifndef WITHOUT_NLS
  #ifndef WITHOUT_NLS

shows that bsd fgrep already fails to ignore case. And if you throw
a few more options to the mix it'd crash, e.g.

  $ env -i LC_CTYPE=en_US.UTF-8 TERM=xterm bsdgrep --color -Fir without_nls usr.bin/grep/
  [...]
  Program received signal SIGSEGV, Segmentation fault.
  0x0000000801007ff2 in memchr (s=0x61167a, c=10, n=18446744073707490297) at /usr/src/lib/libc/string/memchr.c:48
  48                              if (*p++ == (unsigned char)c)
  (gdb) bt
  #0  0x0000000801007ff2 in memchr (s=0x61167a, c=10, n=18446744073707490297) at /usr/src/lib/libc/string/memchr.c:48
  #1  0x0000000801007b03 in __sfvwrite (fp=0x801247770, uio=0x7fffffffd8f0) at /usr/src/lib/libc/stdio/fvwrite.c:170
  #2  0x0000000801007698 in fwrite (buf=0x608c03, size=18446744073709551606, count=1, fp=0x801247770)
      at /usr/src/lib/libc/stdio/fwrite.c:95
  #3  0x0000000000405498 in printline (line=0x7fffffffdb70, sep=58, matches=0x7fffffffd990, m=9)
      at /usr/src/usr.bin/grep/util.c:500
  #4  0x0000000000404f51 in procline (l=0x7fffffffdb70, nottext=0) at /usr/src/usr.bin/grep/util.c:381
  #5  0x000000000040489f in procfile (fn=0x80140b600 "usr.bin/grep/nls/es_ES.ISO8859-1.msg") at /usr/src/usr.bin/grep/util.c:239
  #6  0x00000000004044d7 in grep_tree (argv=0x7fffffffdd30) at /usr/src/usr.bin/grep/util.c:163
  #7  0x0000000000403ea9 in main (argc=5, argv=0x7fffffffdd10) at /usr/src/usr.bin/grep/grep.c:689
资料来源:


此外,即使使用相同的版本,在不同的OSX上似乎也有不同的grep二进制文件:

$ grep --v
grep (BSD grep) 2.5.1-FreeBSD
$ grep --v
grep (BSD grep) 2.5.1-FreeBSD
$ shasum /usr/bin/grep
350ee11e1868e18c9707ea7035184a114f40badf  /usr/bin/grep
$ codesign -dvvv /usr/bin/grep
Executable=/usr/bin/grep
Identifier=com.apple.zgrep
Format=Mach-O thin (x86_64)
CodeDirectory v=20100 size=224 flags=0x0(none) hashes=6+2 location=embedded
Hash type=sha1 size=20
CDHash=1537b3ed49878d5d18482859a37318164a2a40f1
Signature size=4064
Authority=Software Signing
Authority=Apple Code Signing Certification Authority
Authority=Apple Root CA
Info.plist=not bound
Sealed Resources=none
Internal requirements count=2 size=176

然后我切换到我的MacBookAir,它工作了,然后我检查了一下,它安装了自制的Grep2.14。我相信在我了解ack的那一天,我正在测试grep vs ack。因此,这是在Mac电脑上获得适当grep的另一种方法,使用自制软件。这是一种悲哀,因为我使用自制软件获取
gettext
,以便编译grep,而一开始甚至没有检查
grep
。重新找到过去我已经找到的东西的答案是很烦人的。这是brew formula
homebrew/dups/grep
。FWIW,你的例子对我来说很好,因为苹果/usr/bin/grep(也是2.5.1-FreeBSD)是OS X 10.8.4附带的。让我在另一台macbook上用2.5.1测试一下……这很有趣。macbook air(我想是2011年中期的版本;它是一款Sandy Bridge MBA)运行的是Lion 10.7.5,有
grep(GNU grep)2.5.1
,而且处理得也很好。那我一定是在这台机器上坏了什么东西??嘿,谢谢你挖掘这个!我不确定我是否有它的别名来显示颜色或没有,但我肯定更喜欢有颜色(当然脚本不需要它)。我相信它没有别名,但会默认使用颜色。希望在脚本中使用它时(这将使它智能地禁用颜色),它不会出现问题!