Skip to content

Commit f3e8164

Browse files
fix(ci): keep boundary output tails UTF-8 safe (#109167)
* fix(ci): keep boundary output tails UTF-8 safe * fix(ci): preserve split UTF-8 output tails Co-authored-by: qingminlong <[email protected]> --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 14dc557 commit f3e8164

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎scripts/run-additional-boundary-checks.mjs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ export function formatCommand({ command, args }) {
183183
return [command, ...args].join(" ");
184184
}
185185

186+
function decodeUtf8Tail(buffer) {
187+
let start = 0;
188+
while (start < buffer.length && (buffer[start] & 0b1100_0000) === 0b1000_0000) {
189+
start += 1;
190+
}
191+
// Appends are complete JS strings; only a byte slice's leading boundary can be partial.
192+
return buffer.subarray(start).toString("utf8");
193+
}
194+
186195
/**
187196
* Keeps only the tail of noisy check output so failure logs stay bounded.
188197
*/
@@ -197,7 +206,7 @@ export function createBoundedOutputBuffer(maxBytes = DEFAULT_OUTPUT_MAX_BYTES) {
197206
const textBytes = Buffer.byteLength(text);
198207
if (textBytes >= limit) {
199208
const buffer = Buffer.from(text);
200-
const tail = buffer.subarray(buffer.length - limit).toString("utf8");
209+
const tail = decodeUtf8Tail(buffer.subarray(buffer.length - limit));
201210
chunks.splice(0, chunks.length, tail);
202211
bytes = Buffer.byteLength(tail);
203212
truncated = true;
@@ -218,7 +227,7 @@ export function createBoundedOutputBuffer(maxBytes = DEFAULT_OUTPUT_MAX_BYTES) {
218227
}
219228

220229
const buffer = Buffer.from(first);
221-
const tail = buffer.subarray(overflow).toString("utf8");
230+
const tail = decodeUtf8Tail(buffer.subarray(overflow));
222231
chunks[0] = tail;
223232
bytes = chunks.reduce((total, chunk) => total + Buffer.byteLength(chunk), 0);
224233
truncated = true;

‎test/scripts/run-additional-boundary-checks.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ describe("run-additional-boundary-checks", () => {
155155
expect(output.read()).toBe("[output truncated to last 12 bytes]\nsecond-line\n");
156156
});
157157

158+
it("drops split UTF-8 prefixes when one chunk exceeds the output byte cap", () => {
159+
const output = createBoundedOutputBuffer(5);
160+
output.append("old😀new");
161+
162+
expect(output.read()).toBe("[output truncated to last 5 bytes]\nnew");
163+
});
164+
165+
it("drops split UTF-8 prefixes when older buffered output overflows", () => {
166+
const output = createBoundedOutputBuffer(5);
167+
output.append("old😀");
168+
output.append("new");
169+
170+
expect(output.read()).toBe("[output truncated to last 5 bytes]\nnew");
171+
});
172+
158173
it("parses and applies CI shard specs", () => {
159174
expect(parseShardSpec("2/4")).toEqual({ count: 4, index: 1, label: "2/4" });
160175
expect(parseShardSelection("2/4,3/4")).toEqual([
@@ -280,6 +295,30 @@ describe("run-additional-boundary-checks", () => {
280295
expect(result.output).toContain("timed out after 50ms");
281296
});
282297

298+
it("preserves UTF-8 split across process output chunks before trimming the byte tail", async () => {
299+
const script = [
300+
'const bytes = Buffer.from("old😀new");',
301+
"process.stdout.write(bytes.subarray(0, 5));",
302+
"setTimeout(() => process.stdout.end(bytes.subarray(5)), 20);",
303+
].join("");
304+
const result = await runSingleCheck(
305+
{
306+
label: "split-utf8",
307+
command: process.execPath,
308+
args: ["-e", script],
309+
},
310+
{
311+
checkTimeoutMs: 2_000,
312+
cwd: process.cwd(),
313+
env: process.env,
314+
outputMaxBytes: 5,
315+
},
316+
);
317+
318+
expect(result.code).toBe(0);
319+
expect(result.output).toBe("[output truncated to last 5 bytes]\nnew");
320+
});
321+
283322
it("clamps oversized check timers before scheduling", async () => {
284323
const result = await runSingleCheck(
285324
{

0 commit comments

Comments
 (0)