bin/160834: grep: fixes for POSIX conformance

Toby Peterson toby at apple.com
Mon Sep 19 20:40:06 UTC 2011


>Number:         160834
>Category:       bin
>Synopsis:       grep: fixes for POSIX conformance
>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:   Mon Sep 19 20:40:05 UTC 2011
>Closed-Date:
>Last-Modified:
>Originator:     Toby Peterson
>Release:        n/a
>Organization:
Apple Inc.
>Environment:
n/a
>Description:
Standard is at http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html

Fairly straightforward changes, description as follows:

fastgrep.c:
Handle ^$ pattern correctly. Simple test is `echo | grep "^$"` - it should match the single line.

grep.c:
As described in the standard, "The pattern_list's value shall consist of one or more patterns separated by <newline> characters"

util.c:
For the purposes of the standard, inaccessible = nonexistent.

Also don't match the empty end of the line. Simple test for that: `echo abc#def | grep "^[A-Za-z0-9]*$"` should not match anything.

>How-To-Repeat:
See description for test cases.
>Fix:
Patch attached.

Patch attached with submission follows:

Index: fastgrep.c
===================================================================
--- fastgrep.c	(revision 225675)
+++ fastgrep.c	(working copy)
@@ -104,6 +104,10 @@
 		pat++;
 	}
 
+	if (fg->len == 0) {
+		return (-1);
+	}
+
 	if (fg->len >= 14 &&
 	    memcmp(pat, "[[:<:]]", 7) == 0 &&
 	    memcmp(pat + fg->len - 7, "[[:>:]]", 7) == 0) {
Index: grep.c
===================================================================
--- grep.c	(revision 225675)
+++ grep.c	(working copy)
@@ -280,6 +280,21 @@
 }
 
 /*
+ * Adds search patterns from arguments.
+ */
+static void
+add_arg_patterns(const char *arg)
+{
+	char *argcopy, *pattern;
+
+	argcopy = grep_strdup(arg);
+	while ((pattern = strsep(&argcopy, "\n")) != NULL) {
+		add_pattern(pattern, strlen(pattern));
+	}
+	free(argcopy);
+}
+
+/*
  * Reads searching patterns from a file and adds them with add_pattern().
  */
 static void
@@ -461,7 +476,7 @@
 			grepbehave = GREP_EXTENDED;
 			break;
 		case 'e':
-			add_pattern(optarg, strlen(optarg));
+			add_arg_patterns(optarg);
 			needpattern = 0;
 			break;
 		case 'F':
@@ -636,7 +651,7 @@
 
 	/* Process patterns from command line */
 	if (aargc != 0 && needpattern) {
-		add_pattern(*aargv, strlen(*aargv));
+		add_arg_patterns(*aargv);
 		--aargc;
 		++aargv;
 	}
Index: util.c
===================================================================
--- util.c	(revision 225675)
+++ util.c	(working copy)
@@ -194,7 +194,7 @@
 	if (f == NULL) {
 		if (!sflag)
 			warn("%s", fn);
-		if (errno == ENOENT)
+		if (errno == ENOENT || errno == EACCES)
 			notfound = true;
 		return (0);
 	}
@@ -282,7 +282,7 @@
 
 	if (!matchall) {
 		/* Loop to process the whole line */
-		while (st <= l->len) {
+		do {
 			pmatch.rm_so = st;
 			pmatch.rm_eo = l->len;
 
@@ -350,7 +350,7 @@
 
 			if (st == (size_t)pmatch.rm_so)
 				break; 	/* No matches */
-		}
+		} while (st < l->len);
 	} else
 		c = !vflag;
 


>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list