diff options
author | Linus Groh <mail@linusgroh.de> | 2020-05-18 00:28:00 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-18 09:39:55 +0200 |
commit | 476094922b6dbbc252a076aecca9cc0d172e540e (patch) | |
tree | dadd0976793ea60541d74c82f3531ab6b38a4dc8 /Libraries/LibJS/Runtime/Array.cpp | |
parent | 1a1394f7a2f17bb0631cc4aaaa9c78f0cf700312 (diff) | |
download | serenity-476094922b6dbbc252a076aecca9cc0d172e540e.zip |
LibJS: Pass Interpreter& to Value::to_number() et al.
This patch is unfortunately rather large and might make some things feel
bloated, but it is necessary to fix a few flaws in LibJS, primarily
blindly coercing values to numbers without exception checks - i.e.
interpreter.argument(0).to_i32(); // can fail!!!
Some examples where the interpreter would actually crash:
var o = { toString: () => { throw Error() } };
+o;
o - 1;
"foo".charAt(o);
"bar".repeat(o);
To fix this, we now have the following...
to_double(Interpreter&)
to_i32()
to_i32(Interpreter&)
to_size_t()
to_size_t(Interpreter&)
...and a whole lot of exception checking.
There's intentionally no to_double(), use as_double() directly instead.
This way we still can use these convenient utility functions but don't
need to check for exceptions if we are sure the value already is a
number.
Fixes #2267.
Diffstat (limited to 'Libraries/LibJS/Runtime/Array.cpp')
-rw-r--r-- | Libraries/LibJS/Runtime/Array.cpp | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/Libraries/LibJS/Runtime/Array.cpp b/Libraries/LibJS/Runtime/Array.cpp index 2b1d0eae59..b9c6749043 100644 --- a/Libraries/LibJS/Runtime/Array.cpp +++ b/Libraries/LibJS/Runtime/Array.cpp @@ -74,7 +74,9 @@ void Array::length_setter(Interpreter& interpreter, Value value) auto* array = array_from(interpreter); if (!array) return; - auto length = value.to_number(); + auto length = value.to_number(interpreter); + if (interpreter.exception()) + return; if (length.is_nan() || length.is_infinity() || length.as_double() < 0) { interpreter.throw_exception<RangeError>("Invalid array length"); return; |