Skip to content

Commit 7bd8ec8

Browse files
committed
fix(hu): address Copilot round-2 findings on PR #236
- ros.rs: use checked integer conversions (try_from) for JSON->CDR so out-of-range values error instead of silently truncating - state.rs: reject window_ms == 0 in get_tracker_snapshot to avoid divide-by-zero producing inf/NaN rates in measure_hz/measure_bw - engine.rs: serialize connect/endpoints with serde_json instead of format! to be safe against quotes/backslashes in router addresses - mod.rs: strip both hu- and hu_ prefixes for plugin work-dir naming, matching discover_wasm_plugins
1 parent fb81f40 commit 7bd8ec8

4 files changed

Lines changed: 24 additions & 8 deletions

File tree

crates/hiroz-union/src/core/engine.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ impl CoreEngine {
7171
// are long-lived, so the startup cost is invisible to them.)
7272
let mut config = zenoh::Config::default();
7373
config.insert_json5("mode", "\"client\"")?;
74-
config.insert_json5("connect/endpoints", &format!("[\"{}\"]", router_addr))?;
74+
let endpoints_json = serde_json::to_string(&[router_addr])
75+
.map_err(|e| format!("failed to serialize router endpoint: {e}"))?;
76+
config.insert_json5("connect/endpoints", &endpoints_json)?;
7577
config.insert_json5("scouting/multicast/enabled", "false")?;
7678

7779
let session = zenoh::open(config.clone()).await.map_err(|e| {

crates/hiroz-union/src/plugin/wasm/host/ros.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,23 +409,29 @@ fn json_to_dynamic_value(
409409
match ty {
410410
FieldType::Bool => Ok(DynamicValue::Bool(value.as_bool().ok_or("expected bool")?)),
411411
FieldType::Int8 => Ok(DynamicValue::Int8(
412-
value.as_i64().ok_or("expected i8")? as i8
412+
i8::try_from(value.as_i64().ok_or("expected i8")?)
413+
.map_err(|_| "value out of range for i8")?,
413414
)),
414415
FieldType::Int16 => Ok(DynamicValue::Int16(
415-
value.as_i64().ok_or("expected i16")? as i16
416+
i16::try_from(value.as_i64().ok_or("expected i16")?)
417+
.map_err(|_| "value out of range for i16")?,
416418
)),
417419
FieldType::Int32 => Ok(DynamicValue::Int32(
418-
value.as_i64().ok_or("expected i32")? as i32
420+
i32::try_from(value.as_i64().ok_or("expected i32")?)
421+
.map_err(|_| "value out of range for i32")?,
419422
)),
420423
FieldType::Int64 => Ok(DynamicValue::Int64(value.as_i64().ok_or("expected i64")?)),
421424
FieldType::Uint8 => Ok(DynamicValue::Uint8(
422-
value.as_u64().ok_or("expected u8")? as u8
425+
u8::try_from(value.as_u64().ok_or("expected u8")?)
426+
.map_err(|_| "value out of range for u8")?,
423427
)),
424428
FieldType::Uint16 => Ok(DynamicValue::Uint16(
425-
value.as_u64().ok_or("expected u16")? as u16
429+
u16::try_from(value.as_u64().ok_or("expected u16")?)
430+
.map_err(|_| "value out of range for u16")?,
426431
)),
427432
FieldType::Uint32 => Ok(DynamicValue::Uint32(
428-
value.as_u64().ok_or("expected u32")? as u32
433+
u32::try_from(value.as_u64().ok_or("expected u32")?)
434+
.map_err(|_| "value out of range for u32")?,
429435
)),
430436
FieldType::Uint64 => Ok(DynamicValue::Uint64(value.as_u64().ok_or("expected u64")?)),
431437
FieldType::Float32 => Ok(DynamicValue::Float32(

crates/hiroz-union/src/plugin/wasm/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,12 @@ fn load_one(
431431
.file_stem()
432432
.and_then(|s| s.to_str())
433433
.unwrap_or("unknown");
434-
let plugin_stem = plugin_stem.strip_prefix("hu-").unwrap_or(plugin_stem);
434+
// Discovery accepts both `hu-` and `hu_` filename prefixes, so strip either
435+
// to keep `hu-meter.wasm` and `hu_meter.wasm` mapping to the same work dir.
436+
let plugin_stem = plugin_stem
437+
.strip_prefix("hu-")
438+
.or_else(|| plugin_stem.strip_prefix("hu_"))
439+
.unwrap_or(plugin_stem);
435440
let work_dir = plugin_work_dir(plugin_stem);
436441
std::fs::create_dir_all(&work_dir).ok();
437442

crates/hiroz-union/src/plugin/wasm/state.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ impl PluginState {
211211
window_ms: u32,
212212
) -> Result<(usize, usize, f64), String> {
213213
self.require_perm(hu::plugin::types::Permission::MeasureMetrics)?;
214+
if window_ms == 0 {
215+
return Err("window_ms must be greater than zero".to_string());
216+
}
214217
self.ensure_rate_tracker(topic)?;
215218
let tracker = self.rate_trackers.get_mut(topic).unwrap();
216219
tracker.drain_and_trim(window_ms);

0 commit comments

Comments
 (0)