Skip to content

Commit

Permalink
Fixes issue #94521: Make sure to always specify Uint32Array length in…
Browse files Browse the repository at this point in the history
… ctor
  • Loading branch information
alexdima committed Apr 6, 2020
1 parent deb33e0 commit 8918d05
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/vs/workbench/api/common/shared/semanticTokensDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ function fromLittleEndianBuffer(buff: VSBuffer): Uint32Array {
reverseEndianness(uint8Arr);
}
if (uint8Arr.byteOffset % 4 === 0) {
return new Uint32Array(uint8Arr.buffer, uint8Arr.byteOffset);
return new Uint32Array(uint8Arr.buffer, uint8Arr.byteOffset, uint8Arr.length / 4);
} else {
// unaligned memory access doesn't work on all platforms
const data = new Uint8Array(uint8Arr.byteLength);
data.set(uint8Arr);
return new Uint32Array(data.buffer, data.byteOffset);
return new Uint32Array(data.buffer, data.byteOffset, data.length / 4);
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/vs/workbench/test/common/api/semanticTokensDto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import * as assert from 'assert';
import { IFullSemanticTokensDto, IDeltaSemanticTokensDto, encodeSemanticTokensDto, ISemanticTokensDto, decodeSemanticTokensDto } from 'vs/workbench/api/common/shared/semanticTokensDto';
import { VSBuffer } from 'vs/base/common/buffer';

suite('SemanticTokensDto', () => {

Expand Down Expand Up @@ -107,4 +108,29 @@ suite('SemanticTokensDto', () => {
});
});

test('issue #94521: unusual backing array buffer', () => {
function wrapAndSliceUint8Arry(buff: Uint8Array, prefixLength: number, suffixLength: number): Uint8Array {
const wrapped = new Uint8Array(prefixLength + buff.byteLength + suffixLength);
wrapped.set(buff, prefixLength);
return wrapped.subarray(prefixLength, prefixLength + buff.byteLength);
}
function wrapAndSlice(buff: VSBuffer, prefixLength: number, suffixLength: number): VSBuffer {
return VSBuffer.wrap(wrapAndSliceUint8Arry(buff.buffer, prefixLength, suffixLength));
}
const dto: ISemanticTokensDto = {
id: 5,
type: 'full',
data: new Uint32Array([1, 2, 3, 4, 5])
};
const encoded = encodeSemanticTokensDto(dto);

// with misaligned prefix and misaligned suffix
assertEqualFull(<IFullSemanticTokensDto>decodeSemanticTokensDto(wrapAndSlice(encoded, 1, 1)), dto);
// with misaligned prefix and aligned suffix
assertEqualFull(<IFullSemanticTokensDto>decodeSemanticTokensDto(wrapAndSlice(encoded, 1, 4)), dto);
// with aligned prefix and misaligned suffix
assertEqualFull(<IFullSemanticTokensDto>decodeSemanticTokensDto(wrapAndSlice(encoded, 4, 1)), dto);
// with aligned prefix and aligned suffix
assertEqualFull(<IFullSemanticTokensDto>decodeSemanticTokensDto(wrapAndSlice(encoded, 4, 4)), dto);
});
});

0 comments on commit 8918d05

Please sign in to comment.