changeset 595:346ba5b8a488

8026048: Function constructor should convert arguments to String before performing any syntax checks Reviewed-by: jlaskey, hannesw
author sundar
date Tue, 08 Oct 2013 16:46:03 +0200
parents c9921761903b
children 3551855c4f40 8d29733ad609
files src/jdk/nashorn/internal/objects/NativeFunction.java test/script/basic/JDK-8026048.js
diffstat 2 files changed, 44 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/internal/objects/NativeFunction.java	Tue Oct 08 15:53:22 2013 +0200
+++ b/src/jdk/nashorn/internal/objects/NativeFunction.java	Tue Oct 08 16:46:03 2013 +0200
@@ -221,6 +221,7 @@
         final StringBuilder sb = new StringBuilder();
 
         sb.append("(function (");
+        final String funcBody;
         if (args.length > 0) {
             final StringBuilder paramListBuf = new StringBuilder();
             for (int i = 0; i < args.length - 1; i++) {
@@ -230,15 +231,20 @@
                 }
             }
 
+            // now convert function body to a string
+            funcBody = JSType.toString(args[args.length - 1]);
+
             final String paramList = paramListBuf.toString();
             if (! paramList.isEmpty()) {
                 checkFunctionParameters(paramList);
                 sb.append(paramList);
             }
+        } else {
+            funcBody = null;
         }
+
         sb.append(") {\n");
         if (args.length > 0) {
-            final String funcBody = JSType.toString(args[args.length - 1]);
             checkFunctionBody(funcBody);
             sb.append(funcBody);
             sb.append('\n');
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/basic/JDK-8026048.js	Tue Oct 08 16:46:03 2013 +0200
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8026048: Function constructor should convert arguments to String before performing any syntax checks
+ *
+ * @test
+ * @run
+ */
+
+try {
+    Function("-", {toString:function(){throw "err"}})
+} catch (e) {
+    if (e !== "err") {
+        fail("err xpected, got " + e);
+    }
+}