Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

External tools' stderr survives a cap inside a multibyte character (#364)

GGWA6e9fc1a3d2b8
diff --git a/crates/engine/src/tools/external_disasm_tools.rs b/crates/engine/src/tools/external_disasm_tools.rs
index 0a78d336..147d1d46 100644
--- a/crates/engine/src/tools/external_disasm_tools.rs
+++ b/crates/engine/src/tools/external_disasm_tools.rs
@@ -231,7 +231,7 @@ fn text_evidence(
"offset": offset,
"limit": limit,
"output": page.join("\n"),
- "stderr": stderr.get(..stderr.len().min(64 << 10)).unwrap_or(""),
+ "stderr": stderr.get(..stderr.floor_char_boundary(64 << 10)).unwrap_or(""),
}))
.summary(format!("{headline}: {total} lines of output{}.", if out.success { String::new() } else { format!(", exit code {:?}", out.code) }))
.confidence(Confidence::Observed)
@@ -495,4 +495,26 @@ mod tests {
let err = e.call("native_rizin_command_run", serde_json::json!({"path": p, "command": "w x"}), crate::CallOptions::new(crate::Caller::Mcp));
assert!(err.is_err());
}
+
+ #[test]
+ fn stderr_cut_inside_a_multibyte_character_keeps_the_text_before_it() {
+ let mut session = crate::testing::session();
+ let ctx =
+ Ctx { tool: "native_objdump_run", session: &mut session, cancel: crate::CancelToken::default(), progress: &|_| {}, caller: crate::Caller::Cli };
+ // 64 KiB falls inside the arrow in the first case and right after it in the second.
+ for (ascii, kept) in [(65535, 65535), (65533, 65536)] {
+ let mut stderr = vec![b'a'; ascii];
+ stderr.extend_from_slice("\u{2192}tail".as_bytes());
+ let out = Output { code: Some(1), success: false, stderr, ..Default::default() };
+ let subject = Subject { kind: "engine".into(), sha256: None, name: Some("objdump".into()) };
+ let ToolOutput::Evidence(ev) =
+ text_evidence(&ctx, "native_objdump_run", subject, json!({}), Path::new("objdump"), &[], &out, 0, 10, "objdump".into()).unwrap()
+ else {
+ panic!("expected evidence");
+ };
+ let s = ev.result["stderr"].as_str().unwrap();
+ assert_eq!(s.len(), kept);
+ assert!(s.starts_with("aaa"));
+ }
+ }
}
diff --git a/crates/engine/src/tools/radio_tools.rs b/crates/engine/src/tools/radio_tools.rs
index 2f8b5674..52ebe567 100644
--- a/crates/engine/src/tools/radio_tools.rs
+++ b/crates/engine/src/tools/radio_tools.rs
@@ -166,7 +166,7 @@ fn radio_recording_decode(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput
"limit": limit,
"messages": page,
"other_lines": other,
- "stderr": stderr.get(..stderr.len().min(16 << 10)).unwrap_or(""),
+ "stderr": stderr.get(..stderr.floor_char_boundary(16 << 10)).unwrap_or(""),
}))
.summary(format!("rtl_433 decoded {} message(s) from {} kind(s) of device.", msgs.len(), by_model.len()))
.confidence(Confidence::Observed)
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index 9efb769e..5edb9d8a 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -179,6 +179,8 @@ Owner: open · Status: done (branch chore/r0.5-cleanups)
from `DT_HASH` or `DT_GNU_HASH` (moved out of `dart.rs`), `Binary` symbols, imports and exports are
filled from it, `elf::dynamic_symbols` reads versions through `DT_VERSYM`/`DT_VERDEF`/`DT_VERNEED`,
and the prologue scan walks executable segments (#223)
+- [x] Cut external-tool stderr at a character boundary, so a 64 KiB (objdump, rizin) or 16 KiB (rtl_433) cap
+ inside a multibyte character no longer drops the whole stderr (#287).
### R0.8 Tests never touch the real machine
Owner: open · Status: done, except protecting `main` (a maintainer setting)