# HG changeset patch # User sundar # Date 1381243563 -7200 # Node ID 8ebfde8a3436b14778ef4b5a28ad181288cbff9f # Parent 7bceef30eefabf6f5abb9cdabb67b47cf31943aa 8026048: Function constructor should convert arguments to String before performing any syntax checks Reviewed-by: jlaskey, hannesw diff -r 7bceef30eefa -r 8ebfde8a3436 nashorn/src/jdk/nashorn/internal/objects/NativeFunction.java --- a/nashorn/src/jdk/nashorn/internal/objects/NativeFunction.java Tue Oct 08 15:53:22 2013 +0200 +++ b/nashorn/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'); diff -r 7bceef30eefa -r 8ebfde8a3436 nashorn/test/script/basic/JDK-8026048.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nashorn/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); + } +}