From owner-freebsd-bugs@FreeBSD.ORG Mon Oct 25 17:10:10 2010 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9049B10656BB for ; Mon, 25 Oct 2010 17:10:10 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 524CC8FC23 for ; Mon, 25 Oct 2010 17:10:10 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.4/8.14.4) with ESMTP id o9PHAA3Y069625 for ; Mon, 25 Oct 2010 17:10:10 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.4/8.14.4/Submit) id o9PHAAkB069624; Mon, 25 Oct 2010 17:10:10 GMT (envelope-from gnats) Resent-Date: Mon, 25 Oct 2010 17:10:10 GMT Resent-Message-Id: <201010251710.o9PHAAkB069624@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Mark Johnston Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 6E5EA106566B for ; Mon, 25 Oct 2010 17:07:27 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21]) by mx1.freebsd.org (Postfix) with ESMTP id 4222B8FC1C for ; Mon, 25 Oct 2010 17:07:27 +0000 (UTC) Received: from www.freebsd.org (localhost [127.0.0.1]) by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o9PH7RSP045894 for ; Mon, 25 Oct 2010 17:07:27 GMT (envelope-from nobody@www.freebsd.org) Received: (from nobody@localhost) by www.freebsd.org (8.14.3/8.14.3/Submit) id o9PH7Rsu045893; Mon, 25 Oct 2010 17:07:27 GMT (envelope-from nobody) Message-Id: <201010251707.o9PH7Rsu045893@www.freebsd.org> Date: Mon, 25 Oct 2010 17:07:27 GMT From: Mark Johnston To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: bin/151720: sh(1): parser accepts invalid syntax without reporting an error X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Oct 2010 17:10:10 -0000 >Number: 151720 >Category: bin >Synopsis: sh(1): parser accepts invalid syntax without reporting an error >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 Oct 25 17:10:09 UTC 2010 >Closed-Date: >Last-Modified: >Originator: Mark Johnston >Release: 8.1-RELEASE >Organization: >Environment: FreeBSD mark-laptop-bsd.mark-home 8.1-RELEASE FreeBSD 8.1-RELEASE #2: Tue Oct 19 21:08:25 EDT 2010 root@mark-laptop-bsd.mark-home:/usr/obj/usr/src/sys/GENERIC_BCM i386 >Description: One can use sh(1)'s '${#var}' syntax to get the length of the string referenced by var, or 0 if var is unset. However, in this case sh(1) doesn't verify that var is a valid variable name. For instance: $ foo=bar $ echo ${#foo} 3 $ echo ${#foo^^&$} 3 $ echo ${#foo?&$#$^%$#^%#$%^$#%} 3 $ and so on. bash and zsh correctly reject the last two expressions with a 'bad substitution' error as one would expect. >How-To-Repeat: Run sh(1) and try the above examples. >Fix: I've submitted a patch which fixes the parser. I've also attached a regression test to go into tools/regression/bin/sh/errors. With the patch, I get: $ foo=bar $ echo ${#foo} 3 $ echo ${#foo^^&$} ${foo...}: Bad substitution $ echo ${#foo?&$#$^%$#^%#$%^$#%} ${foo...}: Bad substitution $ Note that valid expressions such as '${#?}', '${#-}' and '${##}' are still handled properly with this patch. Patch attached with submission follows: diff --git a/bin/sh/parser.c b/bin/sh/parser.c index c51ab48..40249f5 100644 --- a/bin/sh/parser.c +++ b/bin/sh/parser.c @@ -1283,6 +1283,8 @@ parsesub: { } } } else if (subtype != VSERROR) { + if (subtype == VSLENGTH && c != '}') + subtype = VSERROR; pungetc(); } STPUTC('=', out); diff --git a/tools/regression/bin/sh/errors/bad-parm-exp6.2 b/tools/regression/bin/sh/errors/bad-parm-exp6.2 new file mode 100644 index 0000000..6d4b5c2 --- /dev/null +++ b/tools/regression/bin/sh/errors/bad-parm-exp6.2 @@ -0,0 +1 @@ +${#foo^} diff --git a/tools/regression/bin/sh/errors/bad-parm-exp6.2.stderr b/tools/regression/bin/sh/errors/bad-parm-exp6.2.stderr new file mode 100644 index 0000000..dbf14b5 --- /dev/null +++ b/tools/regression/bin/sh/errors/bad-parm-exp6.2.stderr @@ -0,0 +1 @@ +./errors/bad-parm-exp6.2: ${foo...}: Bad substitution >Release-Note: >Audit-Trail: >Unformatted: