Title: [250275] trunk
Revision
250275
Author
[email protected]
Date
2019-09-23 17:26:29 -0700 (Mon, 23 Sep 2019)

Log Message

Array methods should throw TypeError upon attempting to modify a string
https://bugs.webkit.org/show_bug.cgi?id=201910

Reviewed by Keith Miller.

JSTests:

* stress/array-methods-should-not-modify-string.js: Added.

* mozilla/js1_6/Array/regress-304828.js:
Fix test. Original copy was changed similarly seven years ago:
https://searchfox.org/mozilla-central/source/js/src/tests/non262/Array/regress-304828.js

* stress/phantom-insertion-live-range-should-agree-with-arguments-forwarding.js:
Fix test. `Object.__proto__ = []; Object.shift();` shouldn't be valid JS.

Source/_javascript_Core:

We currently allow Array prototype methods to modify strings that they are called upon in certain cases.
(In particular, we're inconsistent about permitting writes to the length property.)

According to section 22.1.3 of the ES spec, this should result in a TypeError.
https://tc39.es/ecma262/#sec-properties-of-the-array-prototype-object
(Test262 cases are needed, but the key is that all such methods use Set(..., true) which throws on failure.)

* runtime/ArrayPrototype.cpp:
(JSC::putLength):
(JSC::setLength):
Never update the length property of a non-JSArray without checking whether we're actually allowed to.

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (250274 => 250275)


--- trunk/JSTests/ChangeLog	2019-09-24 00:13:38 UTC (rev 250274)
+++ trunk/JSTests/ChangeLog	2019-09-24 00:26:29 UTC (rev 250275)
@@ -1,3 +1,19 @@
+2019-09-23  Ross Kirsling  <[email protected]>
+
+        Array methods should throw TypeError upon attempting to modify a string
+        https://bugs.webkit.org/show_bug.cgi?id=201910
+
+        Reviewed by Keith Miller.
+
+        * stress/array-methods-should-not-modify-string.js: Added.
+
+        * mozilla/js1_6/Array/regress-304828.js:
+        Fix test. Original copy was changed similarly seven years ago:
+        https://searchfox.org/mozilla-central/source/js/src/tests/non262/Array/regress-304828.js
+
+        * stress/phantom-insertion-live-range-should-agree-with-arguments-forwarding.js:
+        Fix test. `Object.__proto__ = []; Object.shift();` shouldn't be valid JS.
+
 2019-09-23  Mark Lam  <[email protected]>
 
         Lazy JSGlobalObject property materialization should not use putDirectWithoutTransition.

Modified: trunk/JSTests/mozilla/js1_6/Array/regress-304828.js (250274 => 250275)


--- trunk/JSTests/mozilla/js1_6/Array/regress-304828.js	2019-09-24 00:13:38 UTC (rev 250274)
+++ trunk/JSTests/mozilla/js1_6/Array/regress-304828.js	2019-09-24 00:26:29 UTC (rev 250275)
@@ -86,7 +86,7 @@
 
 // push
 value  = 'abc';
-expect = 6;
+expect = 'TypeError: Attempted to assign to readonly property.';
 try
 {
   actual = Array.prototype.push.call(value, 'd', 'e', 'f');
@@ -96,7 +96,6 @@
   actual = e + '';
 }
 reportCompare(expect, actual, summary + ': push');
-reportCompare('abc', value, summary + ': push');
 
 // pop
 value  = 'abc';

Added: trunk/JSTests/stress/array-methods-should-not-modify-string.js (0 => 250275)


--- trunk/JSTests/stress/array-methods-should-not-modify-string.js	                        (rev 0)
+++ trunk/JSTests/stress/array-methods-should-not-modify-string.js	2019-09-24 00:26:29 UTC (rev 250275)
@@ -0,0 +1,27 @@
+function shouldThrowTypeError(func) {
+    let error;
+    try {
+        func();
+    } catch (e) {
+        error = e;
+    }
+
+    if (!(error instanceof TypeError))
+        throw new Error('Expected TypeError!');
+}
+
+shouldThrowTypeError(() => { Array.prototype.push.call(''); });
+shouldThrowTypeError(() => { Array.prototype.push.call('', 1); });
+shouldThrowTypeError(() => { Array.prototype.push.call('abc'); });
+shouldThrowTypeError(() => { Array.prototype.push.call('abc', 1); });
+
+shouldThrowTypeError(() => { Array.prototype.pop.call(''); });
+shouldThrowTypeError(() => { Array.prototype.pop.call('abc'); });
+
+shouldThrowTypeError(() => { Array.prototype.shift.call(''); });
+shouldThrowTypeError(() => { Array.prototype.shift.call('', 1); });
+shouldThrowTypeError(() => { Array.prototype.shift.call('abc'); });
+shouldThrowTypeError(() => { Array.prototype.shift.call('abc', 1); });
+
+shouldThrowTypeError(() => { Array.prototype.unshift.call(''); });
+shouldThrowTypeError(() => { Array.prototype.unshift.call('abc'); });

Modified: trunk/JSTests/stress/phantom-insertion-live-range-should-agree-with-arguments-forwarding.js (250274 => 250275)


--- trunk/JSTests/stress/phantom-insertion-live-range-should-agree-with-arguments-forwarding.js	2019-09-24 00:13:38 UTC (rev 250274)
+++ trunk/JSTests/stress/phantom-insertion-live-range-should-agree-with-arguments-forwarding.js	2019-09-24 00:26:29 UTC (rev 250275)
@@ -8,7 +8,7 @@
             const v15 = v10 + 127;
             const v16 = String();
             const v17 = String.fromCharCode(v10,v10,v15);
-            const v19 = Object.shift();
+            const v19 = v3.shift();
             function v23() {
                 let v28 = arguments;
             }

Modified: trunk/Source/_javascript_Core/ChangeLog (250274 => 250275)


--- trunk/Source/_javascript_Core/ChangeLog	2019-09-24 00:13:38 UTC (rev 250274)
+++ trunk/Source/_javascript_Core/ChangeLog	2019-09-24 00:26:29 UTC (rev 250275)
@@ -1,3 +1,22 @@
+2019-09-23  Ross Kirsling  <[email protected]>
+
+        Array methods should throw TypeError upon attempting to modify a string
+        https://bugs.webkit.org/show_bug.cgi?id=201910
+
+        Reviewed by Keith Miller.
+
+        We currently allow Array prototype methods to modify strings that they are called upon in certain cases.
+        (In particular, we're inconsistent about permitting writes to the length property.)
+
+        According to section 22.1.3 of the ES spec, this should result in a TypeError.
+        https://tc39.es/ecma262/#sec-properties-of-the-array-prototype-object
+        (Test262 cases are needed, but the key is that all such methods use Set(..., true) which throws on failure.)
+
+        * runtime/ArrayPrototype.cpp:
+        (JSC::putLength):
+        (JSC::setLength):
+        Never update the length property of a non-JSArray without checking whether we're actually allowed to.
+
 2019-09-23  Mark Lam  <[email protected]>
 
         Lazy JSGlobalObject property materialization should not use putDirectWithoutTransition.

Modified: trunk/Source/_javascript_Core/runtime/ArrayPrototype.cpp (250274 => 250275)


--- trunk/Source/_javascript_Core/runtime/ArrayPrototype.cpp	2019-09-24 00:13:38 UTC (rev 250274)
+++ trunk/Source/_javascript_Core/runtime/ArrayPrototype.cpp	2019-09-24 00:26:29 UTC (rev 250275)
@@ -166,10 +166,14 @@
     RELEASE_AND_RETURN(scope, slot.getValue(exec, index));
 }
 
-static ALWAYS_INLINE bool putLength(ExecState* exec, VM& vm, JSObject* obj, JSValue value)
+static ALWAYS_INLINE void putLength(ExecState* exec, VM& vm, JSObject* obj, JSValue value)
 {
+    auto scope = DECLARE_THROW_SCOPE(vm);
     PutPropertySlot slot(obj);
-    return obj->methodTable(vm)->put(obj, exec, vm.propertyNames->length, value, slot);
+    bool success = obj->methodTable(vm)->put(obj, exec, vm.propertyNames->length, value, slot);
+    RETURN_IF_EXCEPTION(scope, void());
+    if (UNLIKELY(!success))
+        throwTypeError(exec, scope, ReadonlyPropertyWriteError);
 }
 
 static ALWAYS_INLINE void setLength(ExecState* exec, VM& vm, JSObject* obj, unsigned value)
@@ -180,10 +184,8 @@
         jsCast<JSArray*>(obj)->setLength(exec, value, throwException);
         RETURN_IF_EXCEPTION(scope, void());
     }
-    bool success = putLength(exec, vm, obj, jsNumber(value));
-    RETURN_IF_EXCEPTION(scope, void());
-    if (UNLIKELY(!success))
-        throwTypeError(exec, scope, ReadonlyPropertyWriteError);
+    scope.release();
+    putLength(exec, vm, obj, jsNumber(value));
 }
 
 namespace ArrayPrototypeInternal {
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to