Skip to content

Commit a90f543

Browse files
jamesadevineCopilot
andcommitted
fix(compile): use checked u32 arithmetic in migration runner
Address code review finding: the runner did `1 + registry.len() as u32` and `m.to_version == current + 1` without overflow checks. With realistic registries this is unreachable, but rust panics on overflow in debug mode and wraps in release. Switch to checked_add so we surface a clear error either way; preserves existing behavior on all realistic inputs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2ac4560 commit a90f543

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

src/compile/migrations/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,17 @@ pub fn migrate_front_matter_with(
175175
fm: &mut Mapping,
176176
registry: &[&'static Migration],
177177
) -> Result<MigrationReport> {
178-
let target_version = 1 + registry.len() as u32;
178+
// Use checked arithmetic so we surface a clear error rather than
179+
// panic-or-wrap if a registry ever grows past u32::MAX. Realistic
180+
// registries are tiny — this is a "no panic in library code" guard.
181+
let registry_len: u32 = registry
182+
.len()
183+
.try_into()
184+
.ok()
185+
.context("migration registry has more than u32::MAX entries")?;
186+
let target_version = 1u32
187+
.checked_add(registry_len)
188+
.context("migration registry too large: target_version would overflow u32")?;
179189
let mut current = read_schema_version(fm)?;
180190
let from_version = current;
181191

@@ -202,8 +212,11 @@ pub fn migrate_front_matter_with(
202212
current
203213
)
204214
})?;
215+
let next_version = current
216+
.checked_add(1)
217+
.context("migration version overflow: current + 1 exceeds u32::MAX")?;
205218
ensure!(
206-
m.from_version == current && m.to_version == current + 1,
219+
m.from_version == current && m.to_version == next_version,
207220
"migration registry corrupt: expected from_version={} at index {}, found from_version={} to_version={}",
208221
current,
209222
idx,

0 commit comments

Comments
 (0)