sb-dev/openquant / .agents/skills/rust-review
rust-review
VerifiedReview Rust crates, services, or libraries for safe/unsafe boundary issues, memory safety, concurrency hazards, panic-induced denial of service, FFI safety, and async runtime mistakes. Use for a Rust security audit, especially unsafe, FFI, or concurrent code.
upstream/prompts/general/serialize-struct-mismatch-finder.md
View on GitHub---
name: serialize-struct-mismatch-finder
description: Detects manual Serialize impls where the declared element count diverges from the number of fields actually serialized
---
**Finding ID Prefix:** `SERFIELDS`.
**Bug shape:** A manual `Serialize` impl calls `serializer.serialize_struct("X", N)` / `serialize_tuple(N)` (these take a bare `usize`), or `serialize_seq(Some(N))` / `serialize_map(Some(N))` (these take `Option<usize>`), where the declared count does not match the number of `serialize_field`/`serialize_element`/`serialize_entry` calls emitted on every code path. The impact is **format-dependent**, so name the right format:
- **`serialize_seq` / `serialize_map`:** bincode (and other non-self-describing length-prefixed formats) writes the declared `Some(N)` as the element-count prefix, so a wrong `N` truncates or misframes the payload and round-tripping yields different data.
- **`serialize_struct` / `serialize_tuple`:** bincode/postcard **ignore** the declared count entirely (struct/tuple arity is known from the Rust type at decode time), so a wrong `N` there is inert. The count-mismatch corruption instead surfaces in formats that emit a per-struct element header — MessagePack (`rmp-serde`) and CBOR (`ciborium` / `serde_cbor`) — where a wrong header count produces malformed output.
This pattern is caught by the Trail of Bits `wrong_serialize_struct_arg` dylint.
**Gates:**
1. A manual `impl Serialize` uses `serialize_struct`/`serialize_tuple` with an explicit count, or `serialize_seq`/`serialize_map` with an explicit `Some(N)`.
2. The declared count differs from the actual field/element call count on at least one path (conditional `serialize_field`, early return, skipped optional field).
**FPs:**
- Count matches on every reachable path.
- Format is self-describing (JSON, TOML) and ignores the declared length.
- The call is `serialize_struct`/`serialize_tuple` **and** the only target format is bincode/postcard, which discard the declared struct/tuple arity (the corruption requires a count-prefixing format — MessagePack/CBOR — or a `serialize_seq`/`serialize_map` length).
- `#[derive(Serialize)]` is used; count is compiler-generated.
**Patch:** Match the declared count to emitted fields on every path, or eliminate the manual impl with `#[derive(Serialize)]`.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |