bug(utils): tokens_to_base_units validates parts independently but re-parses combined string, giving misleading error on overflow
- Lingua principale
- Rust
- Stelle
- 78
- Fork
- 129
- Merge medio
- 4g 14h
- PR unite (30g)
- 52
Descrizione
### Packages versions
main branch, crates/rust-client
### Bug description
In `tokens_to_base_units` (`crates/rust-client/src/utils.rs`), each numeric part is validated independently via `u64::parse`, but then the parts are concatenated and re-parsed as a combined string:
```rust
// Early validation each part checked independently
for part in &parts {
part.parse::().map_err(TokenParseError::ParseU64)?;
}
// ...later...
let combined = format!("{}{}", integer_part, &fractional_part[0..n_decimals.into()]);
let units = combined.parse::().map_err(TokenParseError::ParseU64)?;
```
The combined string can overflow `u64` even when both parts individually fit. For example:
- `integer_part = "18446744073"` (valid u64)
- `fractional_part = "709551616"` with `n_decimals = 9`
- Combined: `"18446744073709551616"` = `u64::MAX + 1` → parse fails
The error returned is `TokenParseError::ParseU64("invalid digit found in string")` or similar not a meaningful "amount too large" message. The early per-part validation gives false confidence and is a latent refactoring hazard: if someone trusts the early check and removes the late `parse::()`, combined overflow would pass silently.
Affected: `crates/rust-client/src/utils.rs`, `tokens_to_base_units`.
### How can this be reproduced?
Call `tokens_to_base_units("18446744073.709551616", 9)`. Both parts parse as valid u64 individually, but the combined value exceeds u64::MAX. The function returns `Err(TokenParseError::ParseU64(...))` with a misleading message instead of a clear overflow error.
Fix: remove the redundant per-part pre-validation loop and add a dedicated overflow check after the combined parse, returning a descriptive `TokenParseError::AmountTooLarge` (or similar) variant.
### Relevant log output
```shell
```
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
The bug is in `crates/rust-client/src/utils.rs` in the `tokens_to_base_units` function. Start by reading the function to understand the validation and parsing flow. Reproduce the bug with the provided example call. The fix involves removing the redundant per-part validation loop and adding a proper overflow check after the combined parse, likely introducing a new error variant. Run the existing tests to ensure the fix doesn't break anything.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- rust
- Ambito
- cli, tooling
- Tipo di issue
- Bug
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Attiva
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 75/100