Skip to content

Commit b977fe1

Browse files
authored
fix(core): Handle normalization of null prototypes correctly (#15556)
Objects that are created from an Object with a null prototype were not correctly handled before. `Object.getPrototypeOf` of such objects does not return `null` directly but also do not have a constructor. This fix guards against these cases. Closes: #15538
1 parent 6628ad8 commit b977fe1

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

packages/core/src/utils-hoist/normalize.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isSyntheticEvent, isVueViewModel } from './is';
44
import { convertToPlainObject } from './object';
55
import { getFunctionName } from './stacktrace';
66

7-
type Prototype = { constructor: (...args: unknown[]) => unknown };
7+
type Prototype = { constructor?: (...args: unknown[]) => unknown };
88
// This is a hack to placate TS, relying on the fact that technically, arrays are objects with integer keys. Normally we
99
// think of those keys as actual numbers, but `arr['0']` turns out to work just as well as `arr[0]`, and doing it this
1010
// way lets us use a single type in the places where behave as if we are only dealing with objects, even if some of them
@@ -264,7 +264,7 @@ function stringifyValue(
264264
function getConstructorName(value: unknown): string {
265265
const prototype: Prototype | null = Object.getPrototypeOf(value);
266266

267-
return prototype ? prototype.constructor.name : 'null prototype';
267+
return prototype?.constructor ? prototype.constructor.name : 'null prototype';
268268
}
269269

270270
/** Calculates bytes size of input string */

packages/core/test/utils-hoist/normalize.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,17 @@ describe('normalize()', () => {
484484
foo: '[VueViewModel]',
485485
});
486486
});
487+
488+
test('null prototype', () => {
489+
const obj = Object.create(null);
490+
expect(normalize(obj, 0)).toEqual('[null prototype]');
491+
});
492+
493+
test('null prototype base', () => {
494+
const base = Object.create(null);
495+
const obj = Object.create(base);
496+
expect(normalize(obj, 0)).toEqual('[null prototype]');
497+
});
487498
});
488499

489500
describe('can limit object to depth', () => {

0 commit comments

Comments
 (0)