From adcf841a393a4e612b036ec1b8c3a01718e7dd92 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Mon, 11 Jul 2022 05:50:20 -0700 Subject: [PATCH] Be more defensive around SortedList key Fixes #3885 Part of microsoft/vscode#151225 --- src/common/SortedList.ts | 8 ++++++-- src/common/services/DecorationService.ts | 6 +----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/common/SortedList.ts b/src/common/SortedList.ts index 975dc204..9c819959 100644 --- a/src/common/SortedList.ts +++ b/src/common/SortedList.ts @@ -34,6 +34,9 @@ export class SortedList { return false; } const key = this._getKey(value); + if (key === undefined) { + return false; + } let i = this._search(key, 0, this._array.length - 1); if (i === -1) { return false; @@ -75,10 +78,11 @@ export class SortedList { return min; } let mid = Math.floor((min + max) / 2); - if (this._getKey(this._array[mid]) > key) { + const midKey = this._getKey(this._array[mid]); + if (midKey > key) { return this._search(key, min, mid - 1); } - if (this._getKey(this._array[mid]) < key) { + if (midKey < key) { return this._search(key, mid + 1, max); } // Value found! Since keys can be duplicates, move the result index back to the lowest index diff --git a/src/common/services/DecorationService.ts b/src/common/services/DecorationService.ts index 755f13b3..58bff729 100644 --- a/src/common/services/DecorationService.ts +++ b/src/common/services/DecorationService.ts @@ -19,7 +19,7 @@ export class DecorationService extends Disposable implements IDecorationService * while marker line values do change, they should all change by the same amount so this should * never become out of order. */ - private readonly _decorations: SortedList = new SortedList(e => e.marker.line); + private readonly _decorations: SortedList = new SortedList(e => e?.marker.line); private _onDecorationRegistered = this.register(new EventEmitter()); public get onDecorationRegistered(): IEvent { return this._onDecorationRegistered.event; } @@ -28,10 +28,6 @@ export class DecorationService extends Disposable implements IDecorationService public get decorations(): IterableIterator { return this._decorations.values(); } - constructor() { - super(); - } - public registerDecoration(options: IDecorationOptions): IDecoration | undefined { if (options.marker.isDisposed) { return undefined;