Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(adb): count UTF-8 bytes in logcat total_bytes (#2212)

Saim Shafiqueec2d251c0ad8
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<typeof runAdbCapture>
-): Promise<Result<{ stdout: string; stderr: string }, AnalysisError>> => {
+): 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