Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

size fix #120

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion pdl-compiler/src/backends/rust/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ impl Encoder {
});

self.bit_fields.push(BitField {
value: quote!(#array_size as #field_type),
value: quote!((#array_size) as #field_type),
field_type,
shift,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Packet for Bar {
});
}
let x_element_size = x_element_size as u8;
let value = x_size as u8 | (x_element_size << 4);
let value = (x_size) as u8 | (x_element_size << 4);
buf.put_u8(value);
for elem in &self.x {
elem.encode(buf)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Packet for Bar {
});
}
let x_element_size = x_element_size as u8;
let value = x_size as u8 | (x_element_size << 4);
let value = (x_size) as u8 | (x_element_size << 4);
buf.put_u8(value);
for elem in &self.x {
elem.encode(buf)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Packet for Foo {
maximum_value: 0x7 as u64,
});
}
let value = (self.x.len() * 3) as u8 | (self.padding() << 5);
let value = ((self.x.len() * 3)) as u8 | (self.padding() << 5);
buf.put_u8(value);
for elem in &self.x {
buf.put_uint(*elem as u64, 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Packet for Foo {
maximum_value: 0x7 as u64,
});
}
let value = (self.x.len() * 3) as u8 | (self.padding() << 5);
let value = ((self.x.len() * 3)) as u8 | (self.padding() << 5);
buf.put_u8(value);
for elem in &self.x {
buf.put_uint_le(*elem as u64, 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Packet for Bar {
maximum_size: 0xff_ffff_ffff_usize,
});
}
buf.put_uint(x_size as u64, 5);
buf.put_uint((x_size) as u64, 5);
for elem in &self.x {
elem.encode(buf)?;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Packet for Bar {
maximum_size: 0xff_ffff_ffff_usize,
});
}
buf.put_uint_le(x_size as u64, 5);
buf.put_uint_le((x_size) as u64, 5);
for elem in &self.x {
elem.encode(buf)?;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Packet for Foo {
maximum_size: 0xff,
});
}
buf.put_u8(self.payload.len() as u8);
buf.put_u8((self.payload.len()) as u8);
buf.put_slice(&self.payload);
Ok(())
}
Expand Down Expand Up @@ -260,7 +260,7 @@ impl Packet for Bar {
maximum_size: 0xff,
});
}
buf.put_u8(1 as u8);
buf.put_u8((1) as u8);
self.encode_partial(buf)?;
Ok(())
}
Expand Down Expand Up @@ -360,7 +360,7 @@ impl Packet for Baz {
maximum_size: 0xff,
});
}
buf.put_u8(2 as u8);
buf.put_u8((2) as u8);
self.encode_partial(buf)?;
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Packet for Foo {
maximum_size: 0xff,
});
}
buf.put_u8(self.payload.len() as u8);
buf.put_u8((self.payload.len()) as u8);
buf.put_slice(&self.payload);
Ok(())
}
Expand Down Expand Up @@ -260,7 +260,7 @@ impl Packet for Bar {
maximum_size: 0xff,
});
}
buf.put_u8(1 as u8);
buf.put_u8((1) as u8);
self.encode_partial(buf)?;
Ok(())
}
Expand Down Expand Up @@ -360,7 +360,7 @@ impl Packet for Baz {
maximum_size: 0xff,
});
}
buf.put_u8(2 as u8);
buf.put_u8((2) as u8);
self.encode_partial(buf)?;
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Packet for Parent {
maximum_size: 0xff,
});
}
buf.put_u8(self.payload.len() as u8);
buf.put_u8((self.payload.len()) as u8);
buf.put_slice(&self.payload);
Ok(())
}
Expand Down Expand Up @@ -321,7 +321,7 @@ impl Packet for Child {
maximum_size: 0xff,
});
}
buf.put_u8(2 + self.payload.len() as u8);
buf.put_u8((2 + self.payload.len()) as u8);
self.encode_partial(buf)?;
Ok(())
}
Expand Down Expand Up @@ -457,7 +457,7 @@ impl Packet for GrandChild {
maximum_size: 0xff,
});
}
buf.put_u8(2 + self.payload.len() as u8);
buf.put_u8((2 + self.payload.len()) as u8);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This line looks like it could be a fix. However, on a second look,

(2 + self.payload.len()) as u8

and

2 + (self.payload.len() as u8)

should give the same result, right? Will the 2 literal not be type-inferred to be a u8 here?

Is the problem with very large integers (larger than i32?) which can overflow in some way?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The issue is more apparent on this declaration:

little_endian_packets

struct Elem {
    _size_(value) : 8,
    value : 8[],
}

packet Parent {
    _size_(_payload_) : 8,
    _payload_,
}

packet Child : Parent {
    b : Elem,
    d : Elem,
}

The generated code for <Child as Packet>::encode contained the following line which was missing parentheses

buf.put_u8(self.b.encoded_len() + self.d.encoded_len() as u8);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated the added semantic test to properly check this output.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, now I see the problem clearly! Thanks 🙂

buf.put_u16(u16::from(self.quux()));
self.encode_partial(buf)?;
Ok(())
Expand Down Expand Up @@ -581,7 +581,7 @@ impl Packet for GrandGrandChild {
maximum_size: 0xff,
});
}
buf.put_u8(2 + self.payload.len() as u8);
buf.put_u8((2 + self.payload.len()) as u8);
buf.put_u16(u16::from(self.quux()));
self.encode_partial(buf)?;
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Packet for Parent {
maximum_size: 0xff,
});
}
buf.put_u8(self.payload.len() as u8);
buf.put_u8((self.payload.len()) as u8);
buf.put_slice(&self.payload);
Ok(())
}
Expand Down Expand Up @@ -321,7 +321,7 @@ impl Packet for Child {
maximum_size: 0xff,
});
}
buf.put_u8(2 + self.payload.len() as u8);
buf.put_u8((2 + self.payload.len()) as u8);
self.encode_partial(buf)?;
Ok(())
}
Expand Down Expand Up @@ -457,7 +457,7 @@ impl Packet for GrandChild {
maximum_size: 0xff,
});
}
buf.put_u8(2 + self.payload.len() as u8);
buf.put_u8((2 + self.payload.len()) as u8);
buf.put_u16_le(u16::from(self.quux()));
self.encode_partial(buf)?;
Ok(())
Expand Down Expand Up @@ -581,7 +581,7 @@ impl Packet for GrandGrandChild {
maximum_size: 0xff,
});
}
buf.put_u8(2 + self.payload.len() as u8);
buf.put_u8((2 + self.payload.len()) as u8);
buf.put_u16_le(u16::from(self.quux()));
self.encode_partial(buf)?;
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Packet for Foo {
maximum_size: 0xff,
});
}
buf.put_u8(self.payload.len() as u8);
buf.put_u8((self.payload.len()) as u8);
buf.put_slice(&self.payload);
buf.put_u16(self.b());
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Packet for Foo {
maximum_size: 0xff,
});
}
buf.put_u8(self.payload.len() as u8);
buf.put_u8((self.payload.len()) as u8);
buf.put_slice(&self.payload);
buf.put_u16_le(self.b());
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Packet for Test {
maximum_size: 0xff,
});
}
buf.put_u8((self.payload.len() + 1) as u8);
buf.put_u8(((self.payload.len() + 1)) as u8);
buf.put_slice(&self.payload);
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Packet for Test {
maximum_size: 0xff,
});
}
buf.put_u8((self.payload.len() + 1) as u8);
buf.put_u8(((self.payload.len() + 1)) as u8);
buf.put_slice(&self.payload);
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Packet for Foo {
maximum_size: 0xff,
});
}
buf.put_u8(self.payload.len() as u8);
buf.put_u8((self.payload.len()) as u8);
buf.put_slice(&self.payload);
Ok(())
}
Expand Down Expand Up @@ -261,7 +261,7 @@ impl Packet for Bar {
maximum_size: 0xff,
});
}
buf.put_u8(1 as u8);
buf.put_u8((1) as u8);
self.encode_partial(buf)?;
Ok(())
}
Expand Down Expand Up @@ -361,7 +361,7 @@ impl Packet for Baz {
maximum_size: 0xff,
});
}
buf.put_u8(2 as u8);
buf.put_u8((2) as u8);
self.encode_partial(buf)?;
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Packet for Foo {
maximum_size: 0xff,
});
}
buf.put_u8(self.payload.len() as u8);
buf.put_u8((self.payload.len()) as u8);
buf.put_slice(&self.payload);
Ok(())
}
Expand Down Expand Up @@ -261,7 +261,7 @@ impl Packet for Bar {
maximum_size: 0xff,
});
}
buf.put_u8(1 as u8);
buf.put_u8((1) as u8);
self.encode_partial(buf)?;
Ok(())
}
Expand Down Expand Up @@ -361,7 +361,7 @@ impl Packet for Baz {
maximum_size: 0xff,
});
}
buf.put_u8(2 as u8);
buf.put_u8((2) as u8);
self.encode_partial(buf)?;
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Packet for Parent {
maximum_size: 0xff,
});
}
buf.put_u8(self.payload.len() as u8);
buf.put_u8((self.payload.len()) as u8);
buf.put_slice(&self.payload);
Ok(())
}
Expand Down Expand Up @@ -323,7 +323,7 @@ impl Packet for Child {
maximum_size: 0xff,
});
}
buf.put_u8(2 + self.payload.len() as u8);
buf.put_u8((2 + self.payload.len()) as u8);
self.encode_partial(buf)?;
Ok(())
}
Expand Down Expand Up @@ -460,7 +460,7 @@ impl Packet for GrandChild {
maximum_size: 0xff,
});
}
buf.put_u8(2 + self.payload.len() as u8);
buf.put_u8((2 + self.payload.len()) as u8);
buf.put_u16(u16::from(self.quux()));
self.encode_partial(buf)?;
Ok(())
Expand Down Expand Up @@ -585,7 +585,7 @@ impl Packet for GrandGrandChild {
maximum_size: 0xff,
});
}
buf.put_u8(2 + self.payload.len() as u8);
buf.put_u8((2 + self.payload.len()) as u8);
buf.put_u16(u16::from(self.quux()));
self.encode_partial(buf)?;
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Packet for Parent {
maximum_size: 0xff,
});
}
buf.put_u8(self.payload.len() as u8);
buf.put_u8((self.payload.len()) as u8);
buf.put_slice(&self.payload);
Ok(())
}
Expand Down Expand Up @@ -323,7 +323,7 @@ impl Packet for Child {
maximum_size: 0xff,
});
}
buf.put_u8(2 + self.payload.len() as u8);
buf.put_u8((2 + self.payload.len()) as u8);
self.encode_partial(buf)?;
Ok(())
}
Expand Down Expand Up @@ -460,7 +460,7 @@ impl Packet for GrandChild {
maximum_size: 0xff,
});
}
buf.put_u8(2 + self.payload.len() as u8);
buf.put_u8((2 + self.payload.len()) as u8);
buf.put_u16_le(u16::from(self.quux()));
self.encode_partial(buf)?;
Ok(())
Expand Down Expand Up @@ -585,7 +585,7 @@ impl Packet for GrandGrandChild {
maximum_size: 0xff,
});
}
buf.put_u8(2 + self.payload.len() as u8);
buf.put_u8((2 + self.payload.len()) as u8);
buf.put_u16_le(u16::from(self.quux()));
self.encode_partial(buf)?;
Ok(())
Expand Down
Loading