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

from_bits accepts non existing flags #275

Closed
arturoc opened this issue Apr 19, 2022 · 0 comments · Fixed by #276
Closed

from_bits accepts non existing flags #275

arturoc opened this issue Apr 19, 2022 · 0 comments · Fixed by #276

Comments

@arturoc
Copy link

arturoc commented Apr 19, 2022

    #[test]
    fn test_from_bits_edge_cases() {
        bitflags! {
            struct Flags: u8 {
                const A = 0b00000001;
                const BC = 0b00000110;
            }
        }


        let flags = Flags::from_bits(0b00000100);
        assert!(flags.is_none());
    }

Unless I'm missing something this test should pass but it fails cause from_bits accepts flags that are not declared.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6fd4adbddc8b8740cbd35af2306073ca

This is related to this issue in the implementation of iterators in this PR #204 (comment) Using from_bits instead of from_bits_unchecked should allow to produce any valid flags that are not a combination of other flags but at the moment from_bits seems to accept any flag that is included in a combination even if it's not declared. I can try to send a PR.

arturoc added a commit to arturoc/bitflags that referenced this issue Apr 19, 2022
When a bitflags type contains composite flags like:

```rs
bitflags! {
    struct Flags: u8 {
            const A = 0b00000001;
            const BC = 0b00000110;
    }
}
```

from_bits and from_bits_truncate would not work as expected allowing flags that are not declared.

Fixes bitflags#275
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant