Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(assembler): convert action panicked on compacted xbf raws

Sidwebworks04876cf4fd64
diff --git a/apps/assembler/src/actions/convert.rs b/apps/assembler/src/actions/convert.rs
index 3874aa808d..2279f60a32 100644
--- a/apps/assembler/src/actions/convert.rs
+++ b/apps/assembler/src/actions/convert.rs
@@ -78,9 +78,17 @@ pub fn spawn(state: &AppState, job_id: &str, req: ConvertReq) {
let cache_ins = Arc::clone(&cache);
let (lin, ang, optimize) = (req.lin, req.ang, req.optimize);
let res = tokio::task::spawn_blocking(move || {
- // Unit detection scans at most the first 32MB of STEP text.
- let text = read_head_lossy(&tmp_str, 32 * 1024 * 1024)?;
- converter::convert::convert_step(&tmp_str, &text, lin, ang).map(|conv| {
+ // Compacted retained raws are BinXCAF (`.xbf`) — binary, no
+ // STEP header to unit-detect (already mm); running the lossy
+ // text scan on them is wasted work at best.
+ if converter::convert::is_xbf(&tmp_str) {
+ converter::convert::convert_xbf(&tmp_str, lin, ang)
+ } else {
+ // Unit detection scans at most the first 32MB of STEP text.
+ let text = read_head_lossy(&tmp_str, 32 * 1024 * 1024)?;
+ converter::convert::convert_step(&tmp_str, &text, lin, ang)
+ }
+ .map(|conv| {
let glb = if optimize {
optimize_glb(conv.glb)
} else {
diff --git a/crates/converter/src/convert.rs b/crates/converter/src/convert.rs
index 33c120b1ee..8fd12d7bec 100644
--- a/crates/converter/src/convert.rs
+++ b/crates/converter/src/convert.rs
@@ -82,7 +82,7 @@ fn to_node(tree: &occt_bridge::Tree, index: u64) -> AssemblyNode {
/// OCCT BinXCAF (`.xbf`) files start with the ASCII `BINFILE` magic. Content
/// sniff (not extension): transparent zstd means a `.step`-named temp can hold
/// xbf bytes, so the loader must be chosen by what's actually on disk.
-fn is_xbf(path: &str) -> bool {
+pub fn is_xbf(path: &str) -> bool {
use std::io::Read;
let mut head = [0u8; 7];
std::fs::File::open(path)
diff --git a/crates/converter/src/graph.rs b/crates/converter/src/graph.rs
index b969981207..775f0e668a 100644
--- a/crates/converter/src/graph.rs
+++ b/crates/converter/src/graph.rs
@@ -93,8 +93,19 @@ pub fn detect_source_unit(text: &str) -> String {
let si =
SI.get_or_init(|| Regex::new(r"SI_UNIT\s*\(\s*(?:\.(\w+)\.|\$)\s*,\s*\.METRE\.").unwrap());
+ // Lossy decoding expands invalid bytes to 3-byte `�`, so a byte-capped read
+ // can decode to MORE than `cap` bytes — floor to a char boundary or the
+ // slice panics mid-replacement-char (seen on binary xbf heads).
let cap = 32 * 1024 * 1024;
- let text = if text.len() > cap { &text[..cap] } else { text };
+ let text = if text.len() > cap {
+ let mut end = cap;
+ while !text.is_char_boundary(end) {
+ end -= 1;
+ }
+ &text[..end]
+ } else {
+ text
+ };
for m in stmt.captures_iter(text) {
let body = &m[1];
@@ -263,4 +274,15 @@ mod tests {
"mm"
);
}
+
+ #[test]
+ fn source_unit_truncation_respects_char_boundaries() {
+ // A lossy-decoded binary head can exceed the 32MB byte cap (invalid
+ // bytes expand to 3-byte `�`) with a multi-byte char straddling the cap
+ // — slicing at the raw cap used to panic (prod convert on xbf raws).
+ let cap = 32 * 1024 * 1024;
+ let mut text = "a".repeat(cap - 1);
+ text.push('\u{FFFD}'); // bytes cap-1..cap+2 — cap is not a boundary
+ assert_eq!(detect_source_unit(&text), "mm");
+ }
}