Pārlūkot izejas kodu

Fixed stack leak on empty catch block. Fixes #161.

Dmitry Panov 5 gadi atpakaļ
vecāks
revīzija
bfd59704b5
2 mainītis faili ar 17 papildinājumiem un 4 dzēšanām
  1. 1 4
      compiler_stmt.go
  2. 16 0
      compiler_test.go

+ 1 - 4
compiler_stmt.go

@@ -140,13 +140,10 @@ func (c *compiler) compileTryStatement(v *ast.TryStatement) {
 					code[pc] = setLocalP(remap(uint32(instr)))
 				}
 			}
+			c.p.code[start+1] = pop
 			if catchVarIdx, exists := m[0]; exists {
 				c.p.code[start] = setLocal(catchVarIdx)
-				c.p.code[start+1] = pop
 				catchOffset--
-			} else {
-				c.p.code[start+1] = nil
-				catchOffset++
 			}
 		} else {
 			c.scope.accessed = true

+ 16 - 0
compiler_test.go

@@ -2021,6 +2021,22 @@ func TestEvalCallee(t *testing.T) {
 	testScript1(SCRIPT, valueTrue, t)
 }
 
+func TestTryEmptyCatchStackLeak(t *testing.T) {
+	const SCRIPT = `
+	(function() {
+		var f;
+		// Make sure the outer function is not stashless as retStashless masks all stack leaks.
+		(function() {
+			f++;
+		})();
+		try {
+			throw new Error();
+		} catch(e) {}
+	})();
+	`
+	testScript1(SCRIPT, _undefined, t)
+}
+
 // FIXME
 /*
 func TestDummyCompile(t *testing.T) {