shithub: libmujs

Download patch

ref: bd75628fe27044881aa8acb08d06b297530fd638
parent: fea0707d208c05f55b4d23392d261695ce22fa25
author: Avi Halachmi (:avih) <[email protected]>
date: Tue Aug 8 08:15:13 EDT 2017

js_compile: Don't shadow formal arguments with function name.

When a function's name is the same as one of its formal arguments, the
name should refer to the argument, but it was referring to the function.

Other than the obvious bug of referencing an incorrect value, it also
failed to compile in such case in strict mode (but only for lightweight
functions) with "duplicate formal parameter". On non lightweight (and
strict) it was "just" an incorrect value.

--- a/jscompile.c
+++ b/jscompile.c
@@ -1261,14 +1261,18 @@
 	return n;
 }
 
-static void cparams(JF, js_Ast *list)
+static int cparams(JF, js_Ast *list, js_Ast *fname)
 {
+	int shadow = 0;
 	F->numparams = listlength(list);
 	while (list) {
 		checkfutureword(J, F, list->a);
 		addlocal(J, F, list->a, 0);
+		if (fname && !strcmp(fname->string, list->a->string))
+			shadow = 1;
 		list = list->b;
 	}
+	return shadow;
 }
 
 static void cvardecs(JF, js_Ast *node)
@@ -1304,6 +1308,8 @@
 
 static void cfunbody(JF, js_Ast *name, js_Ast *params, js_Ast *body)
 {
+	int shadow;
+
 	F->lightweight = 1;
 	F->arguments = 0;
 
@@ -1318,9 +1324,9 @@
 		if (!strcmp(body->a->string, "use strict"))
 			F->strict = 1;
 
-	cparams(J, F, params);
+	shadow = cparams(J, F, params, name);
 
-	if (name) {
+	if (name && !shadow) {
 		checkfutureword(J, F, name);
 		emit(J, F, OP_CURRENT);
 		if (F->lightweight) {