Skip to content

fix: Add brackets to fix operator precedence when printing version#7

Merged
suyulin merged 1 commit into
suyulin:mainfrom
akiernan:main
Jul 2, 2025
Merged

fix: Add brackets to fix operator precedence when printing version#7
suyulin merged 1 commit into
suyulin:mainfrom
akiernan:main

Conversation

@akiernan

@akiernan akiernan commented Jul 1, 2025

Copy link
Copy Markdown
Contributor

Rust operator precedence has + higher than <<, so the expression was parsed as:

(buf[7] as u16) << (8 + buf[6] as u16)

causing:

alexk@alexk apftool-rs % ./target/debug/afptool-rs ~/Downloads/update.img .
RKFW signature detected

thread 'main' panicked at src/lib.rs:118:9:
attempt to shift left with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Add parentheses to force the shift before the addition and fix the output:

alexk@alexk apftool-rs % ./target/debug/afptool-rs ~/Downloads/update.img .
RKFW signature detected
version: 17.23.4112
family: RK3566
00000066-000401ec BOOT                       (size: 262535)
000401ed-09f169f0 embedded-update.img        (size: 166553604)

Rust operator precedence has `+` higher than `<<`, so the expression was parsed
as:

```
(buf[7] as u16) << (8 + buf[6] as u16)
```

causing:

```
alexk@alexk apftool-rs % ./target/debug/afptool-rs ~/Downloads/update.img .
RKFW signature detected

thread 'main' panicked at src/lib.rs:118:9:
attempt to shift left with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```

Add parentheses to force the shift before the addition and fix the output:

```
alexk@alexk apftool-rs % ./target/debug/afptool-rs ~/Downloads/update.img .
RKFW signature detected
version: 17.23.4112
family: RK3566
00000066-000401ec BOOT                       (size: 262535)
000401ed-09f169f0 embedded-update.img        (size: 166553604)
```

Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
@suyulin

suyulin commented Jul 2, 2025

Copy link
Copy Markdown
Owner

Yes, this fix is correct.

The original code:

(buf[7] as u16) << 8 + buf[6] as u16
has an operator precedence issue. It is effectively interpreted as:

(buf[7] as u16) << (8 + buf[6] as u16)
which means you’re shifting by 8 + buf[6] instead of shifting by 8 bits.

The corrected version:

((buf[7] as u16) << 8) + buf[6] as u16
adds parentheses to ensure the left shift happens first, combining the two bytes into a u16 as intended.

✅ This fix is correct and should be used.
thx your pr

@suyulin suyulin merged commit f4fa69b into suyulin:main Jul 2, 2025
7 checks passed
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.

2 participants