From 7b1ba4bd5cdc2df2e83253e5a7935de70b4ab338 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 25 Apr 2021 22:52:19 +0200 Subject: [PATCH] LibJS: Fallback to undefined if last value in eval() is empty For something like eval(""), the VM's 'last value' is an empty value, which we must not leak. Fixes #6643. --- Userland/Libraries/LibJS/Runtime/GlobalObject.cpp | 2 +- Userland/Libraries/LibJS/Tests/eval-basic.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp index 86425214ac..158d638e2e 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -323,7 +323,7 @@ JS_DEFINE_NATIVE_FUNCTION(GlobalObject::eval) vm.interpreter().execute_statement(global_object, program); if (vm.exception()) return {}; - return vm.last_value(); + return vm.last_value().value_or(js_undefined()); } // 19.2.6.1.1 Encode ( string, unescapedSet ) diff --git a/Userland/Libraries/LibJS/Tests/eval-basic.js b/Userland/Libraries/LibJS/Tests/eval-basic.js index a2a3658db9..43b75028c6 100644 --- a/Userland/Libraries/LibJS/Tests/eval-basic.js +++ b/Userland/Libraries/LibJS/Tests/eval-basic.js @@ -11,6 +11,7 @@ test("basic eval() functionality", () => { test("returns value of last value-producing statement", () => { // See https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation + expect(eval("")).toBeUndefined(); expect(eval("1;;;;;")).toBe(1); expect(eval("1;{}")).toBe(1); expect(eval("1;var a;")).toBe(1);