Skip to content

Commit 85eed83

Browse files
authored
Fix #[derive(Lift)] for enums of 256 cases (#12140)
This commit fixes a longstanding bug in the implementation of `#[derive(Lift)]` which was found by the component_api fuzzer recently. Specifically when an enum or variant had exactly 256 cases the comparison of the discriminant for being out-of-bounds was done in the n-bit space of the discriminant rather than a bit-space that can hold the entire discriminant. The fix here is to compare with a `u32` instead of a `u8` to ensure that if there are 256 variants it actually compares against 256 instead of 0 by accident.
1 parent cdc7bfe commit 85eed83

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

crates/component-macro/src/component.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ impl Expander for LiftExpander {
631631
quote!(u32),
632632
),
633633
};
634-
let discrim_limit = proc_macro2::Literal::usize_unsuffixed(cases.len());
634+
let discrim_limit = proc_macro2::Literal::u32_suffixed(cases.len().try_into().unwrap());
635635

636636
let extract_ty = quote! {
637637
let ty = match ty {
@@ -667,7 +667,7 @@ impl Expander for LiftExpander {
667667
let align = <Self as #wt::component::ComponentType>::ALIGN32;
668668
debug_assert!((bytes.as_ptr() as usize) % (align as usize) == 0);
669669
let discrim = #from_bytes;
670-
if discrim >= #discrim_limit {
670+
if u32::from(discrim) >= #discrim_limit {
671671
#internal::anyhow::bail!("unexpected discriminant: {discrim}");
672672
}
673673
Ok(unsafe {

0 commit comments

Comments
 (0)