Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion vm/devices/virtio/virtio_net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,29 @@ impl Worker {

fn complete_tx_packet(&mut self, id: TxId) -> Result<(), WorkerError> {
let state = &mut self.active_state;
let tx_packet = state.pending_tx_packets[id.0 as usize].take().unwrap();
let idx = id.0 as usize;
let pending_len = state.pending_tx_packets.len();
Comment on lines +1308 to +1309
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id.0 as usize can silently truncate if id.0 is wider than usize (or if this ever builds/runs in a 32-bit environment). Since this function is explicitly handling unexpected IDs, it would be safer to use a checked conversion (e.g., usize::try_from(id.0)) and treat conversion failure the same as out-of-range.

Suggested change
let idx = id.0 as usize;
let pending_len = state.pending_tx_packets.len();
let pending_len = state.pending_tx_packets.len();
let Ok(idx) = usize::try_from(id.0) else {
tracelimit::error_ratelimited!(
tx_id = id.0,
pending_tx_packets_len = pending_len,
"unexpected tx completion for out-of-range packet"
);
return Ok(());
};

Copilot uses AI. Check for mistakes.

let Some(slot) = state.pending_tx_packets.get_mut(idx) else {
tracelimit::error_ratelimited!(
tx_id = id.0,
tx_index = idx,
pending_tx_packets_len = pending_len,
"unexpected tx completion for out-of-range packet"
);
return Ok(());
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an out-of-range TxId, returning Ok(()) may allow the device to continue operating in a potentially corrupted backend/driver state. Consider returning a WorkerError (or otherwise triggering a reset/reconnect path) for the out-of-range case while still tolerating duplicate completions (slot.take() == None) if those can happen benignly.

Suggested change
return Ok(());
return Err(anyhow::anyhow!(
"unexpected tx completion for out-of-range packet: tx_id={}, tx_index={}, pending_tx_packets_len={}",
id.0,
idx,
pending_len
)
.into());

Copilot uses AI. Check for mistakes.
};

let Some(tx_packet) = slot.take() else {
tracelimit::error_ratelimited!(
tx_id = id.0,
tx_index = idx,
pending_tx_packets_len = pending_len,
"unexpected tx completion for already-completed packet"
);
return Ok(());
};

self.virtio_state.tx_queue.complete(tx_packet.work, 0);
self.active_state.stats.tx_packets.increment();
Ok(())
Expand Down
Loading