Skip to content

Commit ad2f465

Browse files
authored
Detect binary files by checking first 256 code units for 0xFFFD (#57008)
1 parent 59b6f78 commit ad2f465

File tree

7 files changed

+31
-8
lines changed

7 files changed

+31
-8
lines changed

src/compiler/scanner.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1795,12 +1795,14 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
17951795

17961796
const ch = codePointAt(text, pos);
17971797
if (pos === 0) {
1798-
// If a file wasn't valid text at all, it will usually be apparent at
1799-
// position 0 because UTF-8 decode will fail and produce U+FFFD.
1798+
// If a file isn't valid text at all, it will usually be apparent
1799+
// in the first few characters because UTF-8 decode will fail and produce U+FFFD.
18001800
// If that happens, just issue one error and refuse to try to scan further;
1801-
// this is likely a binary file that cannot be parsed
1802-
if (ch === CharacterCodes.replacementCharacter) {
1803-
// Jump to the end of the file and fail.
1801+
// this is likely a binary file that cannot be parsed.
1802+
//
1803+
// It's safe to slice the text; U+FFFD can only be produced by an invalid decode,
1804+
// so even if we cut a surrogate pair in half, they wouldn't be U+FFFD.
1805+
if (text.slice(0, 256).includes("\uFFFD")) {
18041806
error(Diagnostics.File_appears_to_be_binary);
18051807
pos = end;
18061808
return token = SyntaxKind.NonTextFileMarkerTrivia;

src/compiler/types.ts

-3
Original file line numberDiff line numberDiff line change
@@ -7532,9 +7532,6 @@ export const enum CharacterCodes {
75327532
mathematicalSpace = 0x205F,
75337533
ogham = 0x1680,
75347534

7535-
// Unicode replacement character produced when a byte sequence is invalid
7536-
replacementCharacter = 0xFFFD,
7537-
75387535
_ = 0x5F,
75397536
$ = 0x24,
75407537

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
TransportStream.ts(1,1): error TS1490: File appears to be binary.
2+
3+
4+
==== TransportStream.ts (1 errors) ====
5+
G@�G@�G@�
6+
7+
!!! error TS1490: File appears to be binary.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [tests/cases/compiler/TransportStream.ts] ////
2+
3+
//// [TransportStream.ts]
4+
G@G@G@
5+
6+
//// [TransportStream.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [tests/cases/compiler/TransportStream.ts] ////
2+
3+
=== TransportStream.ts ===
4+
5+
G@�G@�G@�
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [tests/cases/compiler/TransportStream.ts] ////
2+
3+
=== TransportStream.ts ===
4+
5+
G@�G@�G@�
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
䝀ҒЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄЄ䝀֒ԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅԅ䝀ڒ؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆؆

0 commit comments

Comments
 (0)