Skip to content

Commit d27b43d

Browse files
authored
Merge pull request #1841 from jerch/fix/1840
fix invalid string and buffer index in Linkifier
2 parents a53c6f1 + 0da6300 commit d27b43d

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/Buffer.test.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ describe('Buffer', () => {
355355
let terminal: TestTerminal;
356356

357357
beforeEach(() => {
358-
terminal = new TestTerminal({rows: 5, cols: 10});
358+
terminal = new TestTerminal({rows: 5, cols: 10, scrollback: 5});
359359
});
360360

361361
it('multiline ascii', () => {
@@ -516,9 +516,38 @@ describe('Buffer', () => {
516516
assert.deepEqual([(j / terminal.cols) | 0, j % terminal.cols], bufferIndex);
517517
}
518518
});
519+
520+
it('test fully wrapped buffer up to last char', () => {
521+
const input = Array(6).join('1234567890');
522+
terminal.writeSync(input);
523+
const s = terminal.buffer.iterator(true).next().content;
524+
assert.equal(input, s);
525+
for (let i = 0; i < input.length; ++i) {
526+
const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i);
527+
assert.equal(input[i], terminal.buffer.lines.get(bufferIndex[0]).get(bufferIndex[1])[CHAR_DATA_CHAR_INDEX]);
528+
}
529+
});
530+
531+
it('test fully wrapped buffer up to last char with full width odd', () => {
532+
const input = 'a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301'
533+
+ 'a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301';
534+
terminal.writeSync(input);
535+
const s = terminal.buffer.iterator(true).next().content;
536+
assert.equal(input, s);
537+
for (let i = 0; i < input.length; ++i) {
538+
const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i);
539+
assert.equal(
540+
(!(i % 3))
541+
? input[i]
542+
: (i % 3 === 1)
543+
? input.substr(i, 2)
544+
: input.substr(i - 1, 2),
545+
terminal.buffer.lines.get(bufferIndex[0]).get(bufferIndex[1])[CHAR_DATA_CHAR_INDEX]);
546+
}
547+
});
519548
});
520549
describe('BufferStringIterator', function(): void {
521-
it('iterator does not ovrflow buffer limits', function(): void {
550+
it('iterator does not overflow buffer limits', function(): void {
522551
const terminal = new TestTerminal({rows: 5, cols: 10, scrollback: 5});
523552
const data = [
524553
'aaaaaaaaaa',

src/Buffer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export class Buffer implements IBuffer {
252252
while (stringIndex) {
253253
const line = this.lines.get(lineIndex);
254254
if (!line) {
255-
[-1, -1];
255+
return [-1, -1];
256256
}
257257
for (let i = 0; i < line.length; ++i) {
258258
stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length;

src/Linkifier.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,17 @@ export class Linkifier extends EventEmitter implements ILinkifier {
219219
// also correct regex and string search offsets for the next loop run
220220
stringIndex = text.indexOf(uri, stringIndex + 1);
221221
rex.lastIndex = stringIndex + uri.length;
222+
if (stringIndex < 0) {
223+
// invalid stringIndex (should not have happened)
224+
break;
225+
}
222226

223227
// get the buffer index as [absolute row, col] for the match
224228
const bufferIndex = this._terminal.buffer.stringIndexToBufferIndex(rowIndex, stringIndex);
229+
if (bufferIndex[0] < 0) {
230+
// invalid bufferIndex (should not have happened)
231+
break;
232+
}
225233

226234
const line = this._terminal.buffer.lines.get(bufferIndex[0]);
227235
const char = line.get(bufferIndex[1]);

0 commit comments

Comments
 (0)