Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(rizin): enforce UTF-8 recent output byte limit (#6314)

Raj Aryane28c11f87d03
diff --git a/src/rizin/RizinDebugSessionManager.ts b/src/rizin/RizinDebugSessionManager.ts
index 83752b989..f103b82ba 100644
--- a/src/rizin/RizinDebugSessionManager.ts
+++ b/src/rizin/RizinDebugSessionManager.ts
@@ -1605,7 +1605,7 @@ const waitForCommandFrames = async (
}
};
-const truncateUtf8 = (value: string, maximumBytes: number): string => {
+export const truncateUtf8 = (value: string, maximumBytes: number): string => {
if (maximumBytes <= 0) return "";
// A UTF-16 code unit needs at most three UTF-8 bytes. Buffer.write stops
// before a partial character; only decode bytes it actually initialized.
diff --git a/tests/boundary/providers/RizinInferiorTty.test.ts b/tests/boundary/providers/RizinInferiorTty.test.ts
index 8b23f52f9..7097b3e61 100644
--- a/tests/boundary/providers/RizinInferiorTty.test.ts
+++ b/tests/boundary/providers/RizinInferiorTty.test.ts
@@ -15,6 +15,7 @@ import { afterEach, expect, it, vi } from "vitest";
import {
assertRizinProcessStartTimeStable,
RizinDebugSessionManager,
+ truncateUtf8,
} from "../../../src/rizin/RizinDebugSessionManager.js";
import { evidenceSchema } from "../../../src/domain/evidence.js";
import { TOOL_EFFECTS } from "../../../src/contracts/toolEffects.js";
@@ -316,6 +317,25 @@ it("rejects changed or missing Rizin inferior identity during detach", () => {
);
});
+it("truncates UTF-8 strings by byte limit preserving multi-byte code point boundaries", () => {
+ expect(truncateUtf8("", 10)).toBe("");
+ expect(truncateUtf8("hello", 0)).toBe("");
+ expect(truncateUtf8("hello", -1)).toBe("");
+ expect(truncateUtf8("hello", 3)).toBe("hel");
+ expect(truncateUtf8("hello", 10)).toBe("hello");
+
+ // Multibyte 3-byte CJK character (0xe6 0x96 0x87)
+ // 5 bytes allows only 1 character (3 bytes); the second character would need 6 bytes
+ expect(truncateUtf8("中文", 5)).toBe("中");
+ expect(truncateUtf8("中文", 6)).toBe("中文");
+
+ // 4-byte emoji (🚀 = 0xf0 0x9f 0x9a 0x80)
+ expect(truncateUtf8("🚀🚀", 3)).toBe("");
+ expect(truncateUtf8("🚀🚀", 4)).toBe("🚀");
+ expect(truncateUtf8("🚀🚀", 7)).toBe("🚀");
+ expect(truncateUtf8("🚀🚀", 8)).toBe("🚀🚀");
+});
+
it.skipIf(process.platform !== "linux")(
"retains ownership when cleanup cannot read Rizin stdin",
async () => {