Module Name:    src
Committed By:   kre
Date:           Wed Aug 19 22:41:47 UTC 2020

Modified Files:
        src/bin/sh: parser.c

Log Message:
For now, probably forever, prohibit unquoted $ and ` in the names of
functions being defined (they can still be included if quoted).

If we parsed the way POSIX specifies (leaving the exact input text of
$ and ` expansions unaltered, until required to be expanded) this would
not be needed, as the name of a function being defined does not underbo
parameter, command, or arith expansions, so xxx$3() { : ; } would just
work.   But for many reasons we don't do that (and are unlikely to ever,
though maintaing both forms might be an option someday) - which led to
very obscure behaviour (if sh were compiled in DEBUG mode, even an abort())
and certainly nothing useful.   So just prohibit these uses for now.
(A portable function name must be a "name" so this makes no difference
at all to posix compat applications/scripts).

A doc update is pending (the updated sh.1 also contains updates in other
areas not yet appropriate to commit).


To generate a diff of this commit:
cvs rdiff -u -r1.170 -r1.171 src/bin/sh/parser.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/sh/parser.c
diff -u src/bin/sh/parser.c:1.170 src/bin/sh/parser.c:1.171
--- src/bin/sh/parser.c:1.170	Thu May 14 08:34:17 2020
+++ src/bin/sh/parser.c	Wed Aug 19 22:41:47 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: parser.c,v 1.170 2020/05/14 08:34:17 msaitoh Exp $	*/
+/*	$NetBSD: parser.c,v 1.171 2020/08/19 22:41:47 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
 #else
-__RCSID("$NetBSD: parser.c,v 1.170 2020/05/14 08:34:17 msaitoh Exp $");
+__RCSID("$NetBSD: parser.c,v 1.171 2020/08/19 22:41:47 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -666,6 +666,18 @@ simplecmd(union node **rpp, union node *
 			/* We have a function */
 			consumetoken(TRP);
 			funclinno = plinno;
+			/*
+			 * Make sure there are no unquoted $'s in the
+			 * name (allowing those, not expanding them,
+			 * simply treating '$' as a character, is desireable
+			 * but the parser has converted them to CTLxxx
+			 * chars, and that's not what we want
+			 *
+			 * Fortunately here the user can simply quote
+			 * the name to avoid this restriction.
+			 */
+			if (!noexpand(n->narg.text))
+				synerror("Bad function name (use quotes)");
 			rmescapes(n->narg.text);
 			if (strchr(n->narg.text, '/'))
 				synerror("Bad function name");

Reply via email to