Repository navigation
printer: decode ill-formed UTF-8 as U+FFFD when quoting byte strings #35306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c9aea5e
printer: decode ill-formed UTF-8 as U+FFFD when quoting byte strings
robobun 5254173
review: doc precondition on convert_utf8_bytes_into_utf16; test Bun.f…
robobun a571bfb
bun_core::printer: apply the same U+FFFD decode to the sibling write_…
robobun 8140ce3
js_printer: tighten print_string_literal_utf8 debug_assert to strict …
robobun 5853f83
js_printer: drop bug-history sentence from the Utf8 decode comment
robobun d7725e1
ci: retrigger
robobun ee9d06e
test: bundler coverage for text-loader U+FFFD replacement; trim heade…
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| // `import "./file.txt"` must agree with `Bun.file(f).text()`, `fs.readFileSync(f, "utf8")` | ||
| // and `TextDecoder` on files that are not valid UTF-8. | ||
| // https://github.com/oven-sh/bun/issues/12981 | ||
| describe("text loader decodes ill-formed UTF-8 as U+FFFD", () => { | ||
|
robobun marked this conversation as resolved.
|
||
| const cases: { name: string; bytes: number[] }[] = [ | ||
| { name: "invalid lead bytes", bytes: [0x61, 0xff, 0xfe, 0x62] }, | ||
| { name: "lone continuation byte", bytes: [0x61, 0x80, 0x62] }, | ||
| { name: "2-byte lead then non-continuation", bytes: [0x61, 0xc2, 0x20, 0x62] }, | ||
| { name: "3-byte lead then 1 continuation then ASCII", bytes: [0x61, 0xe0, 0x80, 0x62] }, | ||
| { name: "truncated 2-byte sequence at EOF", bytes: [0x61, 0xc2] }, | ||
| { name: "overlong 4-byte sequence", bytes: [0x61, 0xf0, 0x80, 0x80, 0x80, 0x62] }, | ||
| { name: "surrogate encoded in UTF-8", bytes: [0x61, 0xed, 0xa0, 0x80, 0x62] }, | ||
| { name: "valid 2-byte", bytes: [0x61, 0xc3, 0xa9, 0x62] }, | ||
| { name: "valid 3-byte", bytes: [0x61, 0xe2, 0x82, 0xac, 0x62] }, | ||
| { name: "valid 4-byte", bytes: [0x61, 0xf0, 0x9f, 0x98, 0x80, 0x62] }, | ||
| ]; | ||
|
|
||
| const files: Record<string, Buffer | string> = { | ||
| "entry.ts": ` | ||
| import { readFileSync } from "node:fs"; | ||
| const name = process.argv[2]; | ||
| const { default: loaded } = await import("./" + name + ".txt"); | ||
| const read = readFileSync("./" + name + ".txt", "utf8"); | ||
| const blob = await Bun.file("./" + name + ".txt").text(); | ||
| const cps = (s: string) => [...s].map(c => c.codePointAt(0)); | ||
| console.log(JSON.stringify({ loaded: cps(loaded), read: cps(read), blob: cps(blob) })); | ||
| `, | ||
| }; | ||
| for (let i = 0; i < cases.length; i++) { | ||
| files[`c${i}.txt`] = Buffer.from(cases[i].bytes); | ||
| } | ||
|
|
||
| for (let i = 0; i < cases.length; i++) { | ||
| const { name, bytes } = cases[i]; | ||
| it.concurrent(name, async () => { | ||
| using dir = tempDir("text-loader-invalid-utf8", files); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "entry.ts", `c${i}`], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toBe(""); | ||
| const expected = [...new TextDecoder().decode(Uint8Array.from(bytes))].map(c => c.codePointAt(0)); | ||
| expect(JSON.parse(stdout)).toEqual({ loaded: expected, read: expected, blob: expected }); | ||
| expect(exitCode).toBe(0); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }); | ||
| } | ||
|
robobun marked this conversation as resolved.
|
||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.