Skip to content

Commit

Permalink
node: Fix mask-or operator precedence by replacing + with |
Browse files Browse the repository at this point in the history
A request for using masks and shifts on the known `0-1-2` constants
defining the minor type of a DRM node resulted in the expression to
maintain the original `+` from first subtracting the current node
type followed by adding the requested node type.  As the subtract of
the current node type was replaced by a mask, the resulting `old_id &
ID_MASK + minor_base` expression is interpreted as `old_id & (ID_MASK
+ minor_base)` because of operator precedence, while it would have been
correctly interpreted as `(old_id & ID_MASK) | minor_base` when the
correct `|` OR operator is used.
  • Loading branch information
MarijnS95 committed Nov 1, 2024
1 parent b65efbc commit 3a61424
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ pub fn dev_path(dev: dev_t, ty: NodeType) -> io::Result<PathBuf> {
if let Some(dev_name) = devname(dev) {
let suffix = dev_name.trim_start_matches(|c: char| !c.is_numeric());
if let Ok(old_id) = suffix.parse::<u32>() {
let id_mask = 0b11_1111;
let id = old_id & id_mask + ty.minor_base();
const ID_MASK: u32 = 0b11_1111;
let id = old_id & ID_MASK | ty.minor_base();
let path = PathBuf::from(format!("/dev/dri/{}{}", ty.minor_name_prefix(), id));
if path.exists() {
return Ok(path);
Expand Down Expand Up @@ -363,8 +363,8 @@ pub fn dev_path(dev: dev_t, ty: NodeType) -> io::Result<PathBuf> {
}

let old_id = minor(dev);
let id_mask = 0b11_1111;
let id = old_id & id_mask + ty.minor_base();
const ID_MASK: u32 = 0b11_1111;
let id = old_id & ID_MASK | ty.minor_base();
let path = PathBuf::from(format!("/dev/dri/{}{}", ty.minor_name_prefix(), id));
if path.exists() {
return Ok(path);
Expand Down

0 comments on commit 3a61424

Please sign in to comment.