Skip to content

Commit eebe1eb

Browse files
jakebaileysnovader
authored andcommitted
Prevent detached diagnostics from running off the end of the file (microsoft#55381)
1 parent 08d3e6e commit eebe1eb

File tree

7 files changed

+62
-16
lines changed

7 files changed

+62
-16
lines changed

src/compiler/parser.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,7 @@ namespace Parser {
18031803
return sourceFile;
18041804

18051805
function reportPragmaDiagnostic(pos: number, end: number, diagnostic: DiagnosticMessage) {
1806-
parseDiagnostics.push(createDetachedDiagnostic(fileName, pos, end, diagnostic));
1806+
parseDiagnostics.push(createDetachedDiagnostic(fileName, sourceText, pos, end, diagnostic));
18071807
}
18081808
}
18091809

@@ -2118,7 +2118,7 @@ namespace Parser {
21182118
const lastError = lastOrUndefined(parseDiagnostics);
21192119
let result: DiagnosticWithDetachedLocation | undefined;
21202120
if (!lastError || start !== lastError.start) {
2121-
result = createDetachedDiagnostic(fileName, start, length, message, ...args);
2121+
result = createDetachedDiagnostic(fileName, sourceText, start, length, message, ...args);
21222122
parseDiagnostics.push(result);
21232123
}
21242124

@@ -2470,7 +2470,7 @@ namespace Parser {
24702470
if (lastError) {
24712471
addRelatedInfo(
24722472
lastError,
2473-
createDetachedDiagnostic(fileName, openPosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, tokenToString(openKind), tokenToString(closeKind))
2473+
createDetachedDiagnostic(fileName, sourceText, openPosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, tokenToString(openKind), tokenToString(closeKind))
24742474
);
24752475
}
24762476
}
@@ -4486,7 +4486,7 @@ namespace Parser {
44864486
if (lastError && lastError.code === Diagnostics._0_expected.code) {
44874487
addRelatedInfo(
44884488
lastError,
4489-
createDetachedDiagnostic(fileName, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, "{", "}")
4489+
createDetachedDiagnostic(fileName, sourceText, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, "{", "}")
44904490
);
44914491
}
44924492
}
@@ -8326,7 +8326,7 @@ namespace Parser {
83268326
if (lastError && lastError.code === Diagnostics._0_expected.code) {
83278327
addRelatedInfo(
83288328
lastError,
8329-
createDetachedDiagnostic(fileName, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, "{", "}")
8329+
createDetachedDiagnostic(fileName, sourceText, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, "{", "}")
83308330
);
83318331
}
83328332
}
@@ -9429,7 +9429,7 @@ namespace Parser {
94299429
if (childTypeTag) {
94309430
const lastError = parseErrorAtCurrentToken(Diagnostics.A_JSDoc_typedef_comment_may_not_contain_multiple_type_tags);
94319431
if (lastError) {
9432-
addRelatedInfo(lastError, createDetachedDiagnostic(fileName, 0, 0, Diagnostics.The_tag_was_first_specified_here));
9432+
addRelatedInfo(lastError, createDetachedDiagnostic(fileName, sourceText, 0, 0, Diagnostics.The_tag_was_first_specified_here));
94339433
}
94349434
break;
94359435
}

src/compiler/utilities.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,19 +2150,16 @@ export function createDiagnosticForNodeArrayFromMessageChain(sourceFile: SourceF
21502150
return createFileDiagnosticFromMessageChain(sourceFile, start, nodes.end - start, messageChain, relatedInformation);
21512151
}
21522152

2153-
function assertDiagnosticLocation(file: SourceFile | undefined, start: number, length: number) {
2153+
function assertDiagnosticLocation(sourceText: string, start: number, length: number) {
21542154
Debug.assertGreaterThanOrEqual(start, 0);
21552155
Debug.assertGreaterThanOrEqual(length, 0);
2156-
2157-
if (file) {
2158-
Debug.assertLessThanOrEqual(start, file.text.length);
2159-
Debug.assertLessThanOrEqual(start + length, file.text.length);
2160-
}
2156+
Debug.assertLessThanOrEqual(start, sourceText.length);
2157+
Debug.assertLessThanOrEqual(start + length, sourceText.length);
21612158
}
21622159

21632160
/** @internal */
21642161
export function createFileDiagnosticFromMessageChain(file: SourceFile, start: number, length: number, messageChain: DiagnosticMessageChain, relatedInformation?: DiagnosticRelatedInformation[]): DiagnosticWithLocation {
2165-
assertDiagnosticLocation(file, start, length);
2162+
assertDiagnosticLocation(file.text, start, length);
21662163
return {
21672164
file,
21682165
start,
@@ -8152,8 +8149,12 @@ export function getLocaleSpecificMessage(message: DiagnosticMessage) {
81528149
}
81538150

81548151
/** @internal */
8155-
export function createDetachedDiagnostic(fileName: string, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation {
8156-
assertDiagnosticLocation(/*file*/ undefined, start, length);
8152+
export function createDetachedDiagnostic(fileName: string, sourceText: string, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation {
8153+
if ((start + length) > sourceText.length) {
8154+
length = sourceText.length - start;
8155+
}
8156+
8157+
assertDiagnosticLocation(sourceText, start, length);
81578158
let text = getLocaleSpecificMessage(message);
81588159

81598160
if (some(args)) {
@@ -8222,7 +8223,7 @@ export function attachFileToDiagnostics(diagnostics: DiagnosticWithDetachedLocat
82228223

82238224
/** @internal */
82248225
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithLocation {
8225-
assertDiagnosticLocation(file, start, length);
8226+
assertDiagnosticLocation(file.text, start, length);
82268227

82278228
let text = getLocaleSpecificMessage(message);
82288229

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
parseUnmatchedTypeAssertion.ts(1,2): error TS1109: Expression expected.
2+
parseUnmatchedTypeAssertion.ts(1,12): error TS1141: String literal expected.
3+
parseUnmatchedTypeAssertion.ts(1,12): error TS2304: Cannot find name 'obju2c77'.
4+
parseUnmatchedTypeAssertion.ts(1,21): error TS1109: Expression expected.
5+
parseUnmatchedTypeAssertion.ts(2,1): error TS1005: '{' expected.
6+
7+
8+
==== parseUnmatchedTypeAssertion.ts (5 errors) ====
9+
@<[[import(obju2c77,
10+
~
11+
!!! error TS1109: Expression expected.
12+
~~~~~~~~
13+
!!! error TS1141: String literal expected.
14+
~~~~~~~~
15+
!!! error TS2304: Cannot find name 'obju2c77'.
16+
17+
!!! error TS1109: Expression expected.
18+
19+
20+
!!! error TS1005: '{' expected.
21+
!!! related TS1007 parseUnmatchedTypeAssertion.ts:2:1: The parser expected to find a '}' to match the '{' token here.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [tests/cases/compiler/parseUnmatchedTypeAssertion.ts] ////
2+
3+
//// [parseUnmatchedTypeAssertion.ts]
4+
@<[[import(obju2c77,
5+
6+
7+
//// [parseUnmatchedTypeAssertion.js]
8+
;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [tests/cases/compiler/parseUnmatchedTypeAssertion.ts] ////
2+
3+
=== parseUnmatchedTypeAssertion.ts ===
4+
@<[[import(obju2c77,
5+
>obju2c77 : Symbol(obju2c77)
6+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [tests/cases/compiler/parseUnmatchedTypeAssertion.ts] ////
2+
3+
=== parseUnmatchedTypeAssertion.ts ===
4+
@<[[import(obju2c77,
5+
> : any
6+
><[[import(obju2c77, : [[any]]
7+
8+
> : any
9+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@<[[import(obju2c77,

0 commit comments

Comments
 (0)