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

Error in sample code checking flags #66

Open
timglauertadder opened this issue Jan 9, 2024 · 0 comments · May be fixed by #77
Open

Error in sample code checking flags #66

timglauertadder opened this issue Jan 9, 2024 · 0 comments · May be fixed by #77

Comments

@timglauertadder
Copy link

The sample code contains the follow tests:

.read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0))
.write((flags & O_WRONLY != 0) | (flags & O_RDWR != 0))

However O_RDONLY is 0 so the test is bogus and gives a clippy warning.

The O_XXXXvalues are not bit masks but rather they are values for the two low order bits of the flags value. The tests should probably look somethign like this:

.read((flags & 3) == O_RDONLY || (flags & 3) == O_RDWR)
.write((flags & 3) == O_WRONLY || (flags & 3) == O_RDWR)
tomstokes added a commit to tomstokes/input.rs that referenced this issue Nov 8, 2024
The original code masks flags with O_RDONLY, which is 0:
```
.read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0))
.write((flags & O_WRONLY != 0) | (flags & O_RDWR != 0))
```
This generates Clippy errors because it's equivalent to 0 & 0 != 0.

The correct code uses O_ACCMODE to mask off the lower bits:

```
.read((flags & O_ACCMODE == O_RDONLY) | (flags & O_ACCMODE == O_RDWR))
.write((flags & O_ACCMODE == O_WRONLY) | (flags & O_ACCMODE == O_RDWR))
```

The code could be further simplified with `let access_flags = flags & O_ACCMODE` at the top, but this version maintains the original intent and consistency with similar libinput examples across the internet.

This commit updates all 3 occurrences of the code fragment.

Fixes Smithay#66

Signed-off-by: Tom Stokes <[email protected]>
tomstokes added a commit to tomstokes/input.rs that referenced this issue Nov 8, 2024
The original code masks flags with O_RDONLY, which is 0:

```
.read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0))
.write((flags & O_WRONLY != 0) | (flags & O_RDWR != 0))
```

This generates Clippy errors because it's equivalent to 0 & 0 != 0.

The correct code uses O_ACCMODE to mask off the lower bits:

```
.read((flags & O_ACCMODE == O_RDONLY) | (flags & O_ACCMODE == O_RDWR))
.write((flags & O_ACCMODE == O_WRONLY) | (flags & O_ACCMODE == O_RDWR))
```

The code could be further simplified with `let access_flags = flags & O_ACCMODE` at the top, but this version maintains the original intent and consistency with similar libinput examples across the internet.

This commit updates all 3 occurrences of the code fragment.

Fixes Smithay#66

Signed-off-by: Tom Stokes <[email protected]>
tomstokes added a commit to tomstokes/input.rs that referenced this issue Nov 8, 2024
The original code masks flags with O_RDONLY, which is 0:

> .read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0))

This generates Clippy errors because it's equivalent to 0 & 0 != 0.

The correct code uses O_ACCMODE to mask off the lower bits:

> .read((flags & O_ACCMODE == O_RDONLY) | (flags & O_ACCMODE == O_RDWR))

The code could be further simplified with `let access_flags = flags & O_ACCMODE` at the top, but this version maintains the original intent and consistency with similar libinput examples across the internet.

This commit updates all 3 occurrences of the code fragment.

Fixes Smithay#66

Signed-off-by: Tom Stokes <[email protected]>
@tomstokes tomstokes linked a pull request Nov 8, 2024 that will close this issue
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