From ec2d251c0ad8382e0665546be5ec8f5f1c175962 Mon Sep 17 00:00:00 2001 From: Saim Shafique Date: Mon, 12 Oct 2026 22:56:12 +0500 Subject: [PATCH] fix(adb): count UTF-8 bytes in logcat total_bytes (#2212) * fix(adb): count UTF-8 bytes in logcat total_bytes read_adb_logcat assigned the decoded stdout string's UTF-16 code-unit length to total_bytes, undercounting non-ASCII dumps (a 37-byte CJK line reported 33). Measure the UTF-8 byte length instead, matching the field's byte-unit contract and the repo's Buffer.byteLength convention. Fixes #2175 * fix(adb): count raw captured bytes in logcat total_bytes Carry the pre-decode stdout byte length through runAdb so malformed UTF-8 (replaced by U+FFFD on decode) still reports the true captured byte count instead of the re-encoded replacement size. --- src/adb/AdbProvider.test.ts | 38 +++++++++++++++++++++++++++++++++++++ src/adb/AdbProvider.ts | 9 +++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/adb/AdbProvider.test.ts b/src/adb/AdbProvider.test.ts index 5f36917e4..d1a153848 100644 --- a/src/adb/AdbProvider.test.ts +++ b/src/adb/AdbProvider.test.ts @@ -508,6 +508,44 @@ it("reads a bounded logcat dump with the caller count and buffer", async () => { ]); }); +it("reports UTF-8 byte counts in total_bytes for non-ASCII logcat output", async () => { + const { binary } = await writeAdbStub( + shellStub({ + logcat: + "08-10 13:00:00.000 I/Tag( 1): δΈ­ζ–‡\n08-10 13:00:00.000 I/Tag( 1): πŸ˜€\n", + }), + ); + const result = await evidenceResult(providerOver(binary), { + operation: "read_adb_logcat", + input: { serial: "emulator-5554", count: 500, buffer: "main" }, + }); + expect(result.lines).toEqual([ + "08-10 13:00:00.000 I/Tag( 1): δΈ­ζ–‡", + "08-10 13:00:00.000 I/Tag( 1): πŸ˜€", + ]); + // 30-byte prefix + 6-byte CJK + LF = 37; 30-byte prefix + 4-byte emoji + LF = 35. + expect(result.total_bytes).toBe(72); +}); + +it("counts raw captured bytes in total_bytes for malformed UTF-8 logcat output", async () => { + const { binary } = await writeAdbStub( + () => `if [ "$1" = "version" ]; then ${VERSION_BLOCK}; exit 0; fi +if [ "$1" = "-s" ] && [ "$3" = "shell" ]; then + printf '%b' '08-10 13:00:00.000 I/Tag( 1): A\\377B\\n' + exit 0 +fi +printf 'unexpected arguments: %s\\n' "$@" +exit 1`, + ); + const result = await evidenceResult(providerOver(binary), { + operation: "read_adb_logcat", + input: { serial: "emulator-5554", count: 500, buffer: "main" }, + }); + // 30-byte prefix + "A" + one raw 0xff byte + "B" + LF = 34 captured bytes. + // Re-encoding the decoded U+FFFD replacement would report 36. + expect(result.total_bytes).toBe(34); +}); + it("projects package details from dumpsys package output", async () => { const { binary } = await writeAdbStub( shellStub({ diff --git a/src/adb/AdbProvider.ts b/src/adb/AdbProvider.ts index bb2f49ce0..7113d49bf 100644 --- a/src/adb/AdbProvider.ts +++ b/src/adb/AdbProvider.ts @@ -232,12 +232,17 @@ const runAdbCapture = async ( /** Decode textual adb protocols after the shared byte capture and error boundary. */ const runAdb = async ( ...arguments_: Parameters -): Promise> => { +): Promise< + Result<{ stdout: string; stderr: string; stdoutBytes: number }, AnalysisError> +> => { const captured = await runAdbCapture(...arguments_); if (!captured.ok) return captured; return ok({ stdout: captured.value.stdout.toString("utf8"), stderr: captured.value.stderr.toString("utf8"), + // Byte count of the captured output before UTF-8 decoding, so malformed + // sequences replaced by U+FFFD still report their true captured size. + stdoutBytes: captured.value.stdout.byteLength, }); }; @@ -815,7 +820,7 @@ export class AdbProvider { buffer: request.input.buffer, pid: request.input.pid ?? null, lines: bounded, - total_bytes: ran.value.stdout.length, + total_bytes: ran.value.stdoutBytes, coverage: bounded.length !== lines.length || bounded.length === request.input.count