Skip to content

Use != 0 instead of == 1 for check of masked value #380

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

Merged
merged 1 commit into from
May 2, 2022

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented May 2, 2022

Recently [0] we added a check on a value masked against SEQUENCE_LOCKTIME_DISABLE_FLAG, this check uses x == 1 but x can never be zero since SEQUENCE_LOCKTIME_DISABLE_FLAG is equal to 0x80000000.

Looking at the original commit it looks like we are trying to check for a value higher than the flag value, so we can mask then use x != 0 to see if the flag bit is enabled.

Found by clippy:

error: incompatible bit mask: _ & 2147483648 can never be equal to 1

CC: @darosior, have I understood this correctly man?

[0] commit: commit a3dd9fe840c8cdae84e9d586cd9a9d6c195f0092

Recently [0] we added a check on a value masked against
`SEQUENCE_LOCKTIME_DISABLE_FLAG`, this check uses `x == 1` but x can
never be zero since `SEQUENCE_LOCKTIME_DISABLE_FLAG` is equal to
`0x80000000`.

Looking at the original commit it looks like we are trying to check for
a value _higher_ than the flag value, so we can mask then use `x != 0`
to see if the flag bit is enabled.

Found by clippy:

 error: incompatible bit mask: `_ & 2147483648` can never be equal to `1`

[0] commit: `commit a3dd9fe`
@darosior
Copy link
Contributor

darosior commented May 2, 2022 via email

Copy link
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

Wow. Thanks for catching this bug! ACK b747e51

@sanket1729
Copy link
Member

@apoelstra, does this make for trying out clippy here? I think maybe we can find some middle ground with clippy.

We should not enforce it on every CI run. But we can schedule CI runs every week Sunday night running the clippy CI on the latest tip? This way

  • New contributors won't be annoyed by CI everytime.
  • Every Monday after we see the red cross on latest master tip, we can resolve and fix the issues.

We can debate the schedule period if there is interest.

@sanket1729 sanket1729 merged commit 2468b35 into rust-bitcoin:master May 2, 2022
@tcharding
Copy link
Member Author

tcharding commented May 2, 2022

I am attempting to clear all the clippy warnings here and everywhere in our stack, I'm doing this in an attempt to get clippy running on CI for ever PR - I see no reason not to, is there some reason I'm missing? From my understanding the problems were all related to Rust 1.29, that's why I've waited until now to do this work.

@sanket1729
Copy link
Member

From my understanding the problems were all related to Rust 1.29, that's why I've waited until now to do this work.

FTR, I am in favor of it. But several contributors don't always like the suggestions Clippy presents. It is yet another thing to check while submitting the pull request. And I am sure that we will end up with lots of cases where the last commit is just "Fix up Clippy lints".

In addition to every commit compiling tests, contributors need to run Clippy on every commit which people can forget and would get annoyed whenever the CI yells at low priority things are false positives. Then we need to figure out how to disable some lints that are not annoying etc.

As I said, I am in favor, I am just stating the reasons why people might oppose it.

@tcharding
Copy link
Member Author

oh I forgot about how annoying it is going back over patch sets to fix clippy stuff. Thanks for reminding me, we definitely should find a solution that does not lead to that.

@tcharding tcharding deleted the bitmask branch May 3, 2022 00:09
@LLFourn
Copy link
Contributor

LLFourn commented May 3, 2022

Note one solution to this is to add a commit hook to git which fails to commit unless clippy passes. According to this SO you can commit a directory to rust-miniscript like: .githooks and get people to opt in to opt into the hooks with:

git config --local core.hooksPath .githooks/

@tcharding
Copy link
Member Author

Sweeeet, thanks man.

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 this pull request may close these issues.

4 participants