bin/169500: /bin/expr improperly requires forward slash to be escaped

Ronald F.Guilmette rfg at
Wed Jun 27 21:50:01 UTC 2012

>Number:         169500
>Category:       bin
>Synopsis:       /bin/expr improperly requires forward slash to be escaped
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Jun 27 21:50:01 UTC 2012
>Originator:     Ronald F. Guilmette
>Release:        FreeBSD 8.3-RELEASE amd64

8.3-RELEASE amd64


According to my reading of Sections and of the ANSI/IEEE
standard 1003.2, a forward slash character (/) is an "ordinary" basic
regular expression (BRE) character, and as such can be used in a BRE alone
to match itself.  As far as I can tell, backslash-escaping of this specific
character should not be required within BREs.

I note however that /bin/expr does not allow a naked (non-escaped) forward
slash character within the right-hand operand of the : binary operator.  For
example, /bin/expr complains about a "syntax" error on either of the following
	/bin/expr abcxyz : /
	/bin/expr abcxyz : "/"

I believe that these complaints on the part of /bin/expr are unfounded and
inappropriate, and furthermore that they are not consistant with the 1003.2

In contrast, the following command does not produce a syntax error:

	/bin/expr abcxyz : "\/"

This fact leads me to believe that the FreeBSD implementation of /bin/expr
may perhaps be improperly treating forward slash as some sort of a delimiter
within the BRE operand of the : binary operator.

In my opinion, this should be corrected in order to insure maximal compliance
with the 1003.2 POSIX standard.

***  Please note also that my tests and this report apply strictly and only
to /bin/expr.  However "expr" may perhaps be implemented as a built-in in
some or all shells (e.g. sh, bash, csh, tcsh) and thus, my hope is that
whoever attends to this PR will also take the time to look into the possibility
that fixes are needed also in those shells.


	/bin/expr abcxyz : /
	/bin/expr abcxyz : "/"


I haven't looked at the code, so I cannot propose a fix.  As I have said above
however, my guess is that /bin/expr is in some way treating the right-hand
(BRE) operand of the : binary operator as a thing that either is, or that
will be, at some point in the internal processing, delimited (on both the
left and right) with forward slashes.  If so, then /bin/expr should take care
to pre-escape any user-supplied forward slashes within the user-supplied
BRE operand.

More information about the freebsd-bugs mailing list