C0017 sized-ints-in-contracts¶
Rule: Serde types of a contract crate use sized integers, not usize or isize.
| Code | C0017 (stable, never reused) |
| Id | sized-ints-in-contracts |
| Category | code |
| Source | spirit_level |
| Fix | by hand |
Level per tier¶
| Tier | Level | Reason |
|---|---|---|
| small | off | |
| small-workspace | off | |
| medium | warn |
a contract is read on every party; usize is 32 bits on some of them |
| big | deny |
a contract is read on every party; usize is 32 bits on some of them |
| ai | off | |
| embedded | off | |
| ui | off | |
| egui | off | |
| units-strict | off | |
| units-paranoid | off |
Parameters¶
| Name | Type | Default | Meaning |
|---|---|---|---|
also |
list of strings | [] |
crates held to it besides those the map marks contract |
Why¶
A contract crate holds the serde types that cross a process or node boundary: every party links it and reads the same bytes. usize and isize are 32 bits on one party and 64 on another (firmware and host, wasm and server), so a value that fits on one side overflows or fails to decode on the other.
The lint checks the fields of every struct and enum that derives Serialize or Deserialize in crates the crate map marks contract = true, and in those listed in also.
#[derive(Serialize, Deserialize)]
pub struct Frame {
pub len: usize, // reported
pub seq: u32, // passes
}
How to fix¶
Use a sized integer that holds every value the contract allows: u32, u64, i64. Convert to usize where the value indexes memory.
Configure¶
[lints.sized-ints-in-contracts]
level = "deny"
also = []
reason = "why this project wants it at this level"
Allow one finding in the code with // spirit-level: allow(sized-ints-in-contracts) <reason> (waivers); the code works there too, as in [lints.C0017].