Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Forbid usage of instanceof ArrayBuffer #7653

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions build/eslint-plugin-shaka-rules/arraybuffer-no-instanceof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Disallows usage of instanceof ArrayBuffer',
category: 'Best Practices',
recommended: false,
},
schema: [],
},
create: (ctx) => ({
BinaryExpression: (node) => {
if (node.operator === 'instanceof' && node.right.type === 'Identifier' &&
node.right.name === 'ArrayBuffer') {
ctx.report({
node,
message: 'Do not use instanceof ArrayBuffer, consider using ' +
'ArrayBuffer.isView() if possible',
});
}
},
}),
};
1 change: 1 addition & 0 deletions build/eslint-plugin-shaka-rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
const RULES = [
'arg-comment-spacing',
'array-no-instanceof',
'arraybuffer-no-instanceof',
'private',
];
for (const rule of RULES) {
Expand Down
1 change: 1 addition & 0 deletions lib/net/http_xhr_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ shaka.net.HttpXHRPlugin = class {
};
xhr.onload = (event) => {
const headers = shaka.net.HttpXHRPlugin.headersToGenericObject_(xhr);
// eslint-disable-next-line shaka-rules/arraybuffer-no-instanceof
goog.asserts.assert(xhr.response instanceof ArrayBuffer,
'XHR should have a response by now!');
const xhrResponse = xhr.response;
Expand Down
18 changes: 10 additions & 8 deletions lib/util/buffer_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ shaka.util.BufferUtils = class {
* @private
*/
static unsafeGetArrayBuffer_(view) {
if (view instanceof ArrayBuffer) {
return view;
if (!ArrayBuffer.isView(view)) {
return /** @type {!ArrayBuffer} */(view);
} else {
return view.buffer;
return /** @type {!ArrayBufferView} */(view).buffer;
}
}

Expand All @@ -79,17 +79,19 @@ shaka.util.BufferUtils = class {
* @export
*/
static toArrayBuffer(view) {
if (view instanceof ArrayBuffer) {
return view;
if (!ArrayBuffer.isView(view)) {
return /** @type {!ArrayBuffer} */(view);
} else {
if (view.byteOffset == 0 && view.byteLength == view.buffer.byteLength) {
const aView = /** @type {!ArrayBufferView} */(view);
if (aView.byteOffset == 0 &&
aView.byteLength == aView.buffer.byteLength) {
// This is a TypedArray over the whole buffer.
return view.buffer;
return aView.buffer;
}
// This is a "view" on the buffer. Create a new buffer that only contains
// the data. Note that since this isn't an ArrayBuffer, the "new" call
// will allocate a new buffer to hold the copy.
return new Uint8Array(view).buffer;
return new Uint8Array(aView).buffer;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/util/object_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ shaka.util.ObjectUtils = class {

// This covers Uint8Array and friends, even without a TypedArray
// base-class constructor.
const isTypedArray = val.buffer instanceof ArrayBuffer;
const isTypedArray = ArrayBuffer.isView(val);
if (isTypedArray) {
return val;
}
Expand Down
1 change: 1 addition & 0 deletions test/test/util/canned_idb.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ shaka.test.CannedIDB = class {
* @private
*/
static replacer_(dummyArrayBuffers, key, value) {
// eslint-disable-next-line shaka-rules/arraybuffer-no-instanceof
if (value instanceof ArrayBuffer) {
/** @type {string} */
let data;
Expand Down
Loading