From nobody@FreeBSD.org  Tue Aug 13 13:31:02 2013
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115])
	(using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by hub.freebsd.org (Postfix) with ESMTP id 5538EFF6
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 13 Aug 2013 13:31:02 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from oldred.freebsd.org (oldred.freebsd.org [8.8.178.121])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by mx1.freebsd.org (Postfix) with ESMTPS id 42CA026F6
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 13 Aug 2013 13:31:02 +0000 (UTC)
Received: from oldred.freebsd.org ([127.0.1.6])
	by oldred.freebsd.org (8.14.5/8.14.7) with ESMTP id r7DDV1EL051831
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 13 Aug 2013 13:31:01 GMT
	(envelope-from nobody@oldred.freebsd.org)
Received: (from nobody@localhost)
	by oldred.freebsd.org (8.14.5/8.14.5/Submit) id r7DDV1LA051830;
	Tue, 13 Aug 2013 13:31:01 GMT
	(envelope-from nobody)
Message-Id: <201308131331.r7DDV1LA051830@oldred.freebsd.org>
Date: Tue, 13 Aug 2013 13:31:01 GMT
From: kenorb <kenorb@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: grep crashes at memchr/__sfvwrite when using colors
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         181263
>Category:       bin
>Synopsis:       grep(1) crashes at memchr/__sfvwrite when using colors
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Aug 13 13:40:00 UTC 2013
>Closed-Date:    
>Last-Modified:  Thu Aug 15 03:11:06 UTC 2013
>Originator:     kenorb
>Release:        OSX
>Organization:
>Environment:
>Description:
BSD grep is crashing on different behavior when using --colors parameter.
Check the below examples.

Related posts:
http://lists.freebsd.org/pipermail/freebsd-current/2011-August/026502.html
http://stackoverflow.com/questions/17247142/grep-f-on-os-x-produces-segfault/18210372
>How-To-Repeat:
1.
$ echo 'abcd*' | grep --color=auto -e ".*" -e a
Segmentation fault: 11

2.
$ echo "fojeiwuroiuwet\nljfajsljkfabcdddjlfkajlkj\nabcaaa\nzzzabc\n" | grep --color -f grepfile
Segmentation fault: 11

3.
echo 'abcd*' > grepfile
echo 'abc$' >> grepfile
echo '^abc' >> grepfile
echo "fojeiwuroiuwet\nljfajsljkfabcdddjlfkajlkj\nabcaaa\nzzzabc\n" | grep --color=auto -f grepfile
>Fix:
Don't use colors e.g.
--color=never

>Release-Note:
>Audit-Trail:

From: "Rafal W." <kenorb@gmail.com>
To: bug-followup@freebsd.org
Cc: Rafal Wieczorek <kenorb@gmail.com>
Subject: Re: bin/181263: grep crashes at memchr/__sfvwrite when using colors
Date: Tue, 13 Aug 2013 14:46:58 +0100

 --bcaec520f4d5be5d4e04e3d47ac5
 Content-Type: text/plain; charset=ISO-8859-1
 
 Backtraces:
 
 Unix:
 
 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
 
 OSX:
 Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
 0   libsystem_c.dylib               0x00007fff8a8da1b3 memchr + 15
 1   libsystem_c.dylib               0x00007fff8a90b45a __sfvwrite + 578
 2   libsystem_c.dylib               0x00007fff8a90b861 fwrite + 114
 3   grep                            0x000000010a4a3138 0x10a4a0000 + 12600
 4   grep                            0x000000010a4a2988 0x10a4a0000 + 10632
 5   grep                            0x000000010a4a1c28 0x10a4a0000 + 7208
 6   libdyld.dylib                   0x00007fff8daf77e1 start + 1
 
 --bcaec520f4d5be5d4e04e3d47ac5--
>Unformatted:
