8177691: Labeled break in catch and finally works wrongly, when invoked through nashorn

Added support to check if the block contains goto statements before flagging it as terminal

Reviewed-by: hannesw, jlaskey
This commit is contained in:
Srinivas Dama 2017-09-01 06:01:08 +05:30 committed by Srinivas Dama
parent 1205adaedf
commit 43c205bd1c
4 changed files with 88 additions and 3 deletions
nashorn
src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal
test/script/basic

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2017, 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
@ -99,6 +99,12 @@ public class Block extends Node implements BreakableNode, Terminal, Flags<Block>
*/
public static final int IS_SWITCH_BLOCK = 1 << 7;
/**
* Is this block tagged as breakable based on its contents
* (block having labelled break statement)
*/
public static final int IS_BREAKABLE = 1 << 8;
/**
* Constructor
*
@ -315,7 +321,7 @@ public class Block extends Node implements BreakableNode, Terminal, Flags<Block>
*/
@Override
public boolean isTerminal() {
return getFlag(IS_TERMINAL);
return getFlag(IS_TERMINAL) && !getFlag(IS_BREAKABLE);
}
/**

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2017, 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
@ -2274,6 +2274,11 @@ public class Parser extends AbstractParser implements Loggable {
//targetNode is what we are breaking out from.
final String labelName = labelNode == null ? null : labelNode.getLabelName();
final ParserContextBreakableNode targetNode = lc.getBreakable(labelName);
if( targetNode instanceof ParserContextBlockNode) {
targetNode.setFlag(Block.IS_BREAKABLE);
}
if (targetNode == null) {
throw error(AbstractParser.message("illegal.break.stmt"), breakToken);
}

@ -0,0 +1,71 @@
/*
* Copyright (c) 2017, 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-8177691: Labeled break in catch and finally works wrongly, when invoked through nashorn
*
* @test
* @run
*/
function box1() {
label: {
try {
throw 1;
}
catch (e) {
break label;
}
throw 2;
}
return 'OK';
};
function box2() {
label: {
try {
throw 1;
}
finally {
break label;
}
throw 2;
}
return 'OK';
};
function box3() {
label: {
try {
throw 1;
}
finally {
break label;
}
}
return 'OK';
};
print(box1());
print(box2());
print(box3());

@ -0,0 +1,3 @@
OK
OK
OK