Skip to content

Commit 1cb075c

Browse files
authoredFeb 3, 2025
Fix nested packet size (#17)
We were previously double-counting nested packet sizes: both in the parent packet's `MINIMUM_SIZE`, and again when calling the inner `packet_size()`. This wasn't an issue when the only nested packets were `Repeated<..>` (with a minimum size of 0), but #16 lets us nest a **single** header within another.
1 parent 5aa087a commit 1cb075c

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed
 

‎ingot-macros/src/packet/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -982,11 +982,12 @@ impl StructParseDeriveCtx {
982982
let idx = syn::Index::from(i);
983983
match field {
984984
ChunkState::VarWidth(id) | ChunkState::Parsable(id) => {
985+
let ty = &self.validated[id].borrow().user_ty;
985986
zc_len_checks.push(quote! {
986-
self.#idx.packet_length()
987+
self.#idx.packet_length() - <#ty as ::ingot::types::HeaderLen>::MINIMUM_LENGTH
987988
});
988989
owned_len_checks.push(quote! {
989-
self.#id.packet_length()
990+
self.#id.packet_length() - <#ty as ::ingot::types::HeaderLen>::MINIMUM_LENGTH
990991
});
991992
}
992993
ChunkState::FixedWidth { .. } => {}

‎ingot/src/tests.rs

+30
Original file line numberDiff line numberDiff line change
@@ -563,3 +563,33 @@ fn accessor_functions_safely() {
563563
a.destination = 8989.into();
564564
assert_eq!(u16::from(a.destination), 8989);
565565
}
566+
567+
#[derive(Ingot)]
568+
pub struct OuterPacket {
569+
pub bla: u8,
570+
571+
#[ingot(subparse())]
572+
pub next_packet: InnerPacket,
573+
}
574+
575+
#[derive(Clone, Ingot)]
576+
pub struct InnerPacket {
577+
pub boo: u8,
578+
#[ingot(var_len = "boo")]
579+
pub varying: alloc::vec::Vec<u8>,
580+
}
581+
582+
#[test]
583+
fn nested_packet_size() {
584+
let p = OuterPacket {
585+
bla: 1,
586+
next_packet: InnerPacket { boo: 2, varying: vec![1, 2] },
587+
};
588+
assert_eq!(p.packet_length(), 4);
589+
590+
let p = OuterPacket {
591+
bla: 1,
592+
next_packet: InnerPacket { boo: 0, varying: vec![] },
593+
};
594+
assert_eq!(p.packet_length(), 2);
595+
}

0 commit comments

Comments
 (0)