From f120c40de34ecf7ff193b7160f804f42d54e4c28 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Thu, 5 Mar 2020 22:30:36 +0100 Subject: [PATCH] fix NPE with a string switch if the default case is not the last case block. --- .../jwebassembly/module/BranchManger.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/BranchManger.java b/src/de/inetsoftware/jwebassembly/module/BranchManger.java index 2731965..fd42992 100644 --- a/src/de/inetsoftware/jwebassembly/module/BranchManger.java +++ b/src/de/inetsoftware/jwebassembly/module/BranchManger.java @@ -608,13 +608,20 @@ class BranchManger { if( start > node.endPos ) { if( end > branch.endPos ) { BranchNode parentNode = branch; - while( end > parentNode.endPos ) { + while( parentNode != null && end > parentNode.endPos ) { parentNode = parentNode.parent; } - BranchNode middleNode = new BranchNode( startPosition, parsedBlock.endPosition, WasmBlockOperator.BLOCK, WasmBlockOperator.END ); - BranchNode child = parentNode.remove( 0 ); - parentNode.add( middleNode ); - middleNode.add( child ); + BranchNode middleNode = new BranchNode( startPosition, end, WasmBlockOperator.BLOCK, WasmBlockOperator.END ); + if( parentNode != null ) { + BranchNode child = parentNode.remove( 0 ); + parentNode.add( middleNode ); + middleNode.add( child ); + } else { + // occur if the default is not the last case in the switch + middleNode.add( blockNode ); + blockNode = middleNode; + lastPosition = end; + } cases[posCount].block++; } break; @@ -650,7 +657,7 @@ class BranchManger { WasmBlockOperator startOp; if( parsedBlock.op == JavaBlockOperator.GOTO ) { startOp = WasmBlockOperator.BR; - lastPosition = end; + lastPosition = Math.max( lastPosition, end ); } else { startOp = WasmBlockOperator.BR_IF; }