-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make make_bitflags! sound even when dirty tricks are employed
Previously, the macro relied on an expression of the form Enum::Variant always being a variant of the enum. However, it may also be an associated integer constant, in which case we don't have any guarantee that the value only contains bits valid for this enum. Thus, adverserial input to the macro could create a `BitFlags<Enum>` with bits that don't correspond to any of the enum variants. Iterating over such a value would then cause UB. The debug formatter is also affected, as internally it iterates over the value being formatted.
- Loading branch information
1 parent
467fe56
commit 24b13aa
Showing
3 changed files
with
32 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use enumflags2::{bitflags, make_bitflags}; | ||
|
||
#[bitflags] | ||
#[repr(u8)] | ||
#[derive(Copy, Clone, Debug)] | ||
enum Test { | ||
A = 1, | ||
B = 2, | ||
} | ||
|
||
impl Test { | ||
const C: u8 = 69; | ||
} | ||
|
||
fn main() { | ||
let x = make_bitflags!(Test::{C}); | ||
dbg!(x); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error[E0308]: mismatched types | ||
--> ui/sneaky_make_bitflags.rs:16:13 | ||
| | ||
16 | let x = make_bitflags!(Test::{C}); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected `Test`, found `u8` | ||
| expected due to this | ||
| | ||
= note: this error originates in the macro `make_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info) |