|
| 1 | +# Code Review |
| 2 | + |
| 3 | +## Findings (ordered by severity) |
| 4 | + |
| 5 | +- [High] Signed 24-bit encode/decode is incorrect: decode zero-extends and encode rejects negative values. |
| 6 | + - mavlink-core/src/bytes.rs:145 |
| 7 | + - mavlink-core/src/bytes_mut.rs:121 |
| 8 | + - Impact: negative `int24` values are parsed as large positives and serialization panics for valid negative values. |
| 9 | + - Fix: sign-extend on read and use a negative MIN (-(1 << 23)) on write; consider unit tests for `i24` round-trips. |
| 10 | + |
| 11 | +- [High] Build script requires `git`, mutates the submodule, and ignores non-zero exit statuses. |
| 12 | + - mavlink/build/main.rs:13 |
| 13 | + - mavlink/build/main.rs:19 |
| 14 | + - mavlink/build/main.rs:35 |
| 15 | + - Impact: builds can fail in offline/crates.io environments or produce inconsistent definitions; patch failures are silently ignored because only spawn errors are checked. |
| 16 | + - Fix: ship patched XML definitions (or pre-generated Rust), avoid VCS operations in `build.rs`, and check `status.success()` for `git` calls if they remain. |
| 17 | + |
| 18 | +- [Medium] `tokio-1` and `embedded` are documented as incompatible but not enforced, and they define duplicate async APIs. |
| 19 | + - mavlink-core/src/lib.rs:875 |
| 20 | + - mavlink-core/src/lib.rs:899 |
| 21 | + - Impact: enabling both features produces duplicate symbol errors and confusing APIs. |
| 22 | + - Fix: add a `compile_error!` guard for `cfg(all(feature = "tokio-1", feature = "embedded"))` or gate one set of functions with `not(feature = "embedded")`. |
| 23 | + |
| 24 | +- [Medium] UDP (sync/async) `recv` loops swallow all errors, and file `recv` ignores non-EOF I/O errors. |
| 25 | + - mavlink-core/src/connection/udp.rs:90 |
| 26 | + - mavlink-core/src/async_connection/udp.rs:92 |
| 27 | + - mavlink-core/src/connection/file.rs:45 |
| 28 | + - Impact: persistent I/O errors turn into infinite loops or busy-spins; callers cannot observe disconnects or socket failures. |
| 29 | + - Fix: only ignore parse/CRC errors; propagate `MessageReadError::Io` (except maybe `WouldBlock`/`UnexpectedEof` where appropriate). |
| 30 | + |
| 31 | +- [Low] `PeekReader`/`AsyncPeekReader` rejects exact buffer-size reads even though docs say “more than BUFFER_SIZE”. |
| 32 | + - mavlink-core/src/peek_reader.rs:141 |
| 33 | + - mavlink-core/src/async_peek_reader.rs:139 |
| 34 | + - Impact: `peek_exact(BUFFER_SIZE)` panics; API behavior does not match documentation. |
| 35 | + - Fix: change `< BUFFER_SIZE` to `<= BUFFER_SIZE` and update docs/tests. |
| 36 | + |
| 37 | +- [Low] Address format docs and examples use `udpbcast`, but the parser accepts `udpcast`. |
| 38 | + - mavlink-core/src/connectable.rs:78 |
| 39 | + - mavlink-core/src/async_connection/mod.rs:91 |
| 40 | + - mavlink/examples/mavlink-dump/src/main.rs:10 |
| 41 | + - Impact: user confusion and copy/paste failures. |
| 42 | + - Fix: accept both or standardize documentation and CLI help on one string. |
| 43 | + |
| 44 | +## Simplification / existing-crate opportunities |
| 45 | + |
| 46 | +- Replace custom `bytes`/`bytes_mut` helpers with `byteorder::ByteOrder` and/or `bytes::Buf`/`BufMut` for most primitives, keeping a small custom helper only for `u24/i24`. This reduces bespoke parsing code and risk (the current i24 bug is a good example). |
| 47 | + - mavlink-core/src/bytes.rs |
| 48 | + - mavlink-core/src/bytes_mut.rs |
| 49 | + - mavlink-bindgen/src/parser.rs:806 |
| 50 | + |
| 51 | +- Drop `utils::RustDefault` in favor of `Default` (arrays implement `Default` on Rust 1.80). Update codegen to use `#[serde(default)]` instead of a custom default function. |
| 52 | + - mavlink-core/src/utils.rs |
| 53 | + - mavlink-bindgen/src/parser.rs:752 |
| 54 | + - mavlink/src/lib.rs:68 |
| 55 | + |
| 56 | +- Consider splitting the 2300-line `mavlink-core/src/lib.rs` into focused modules (frame structs, parsing, write APIs, connection glue) or using small macros for the repeated v1/v2 + sync/async variants to reduce duplication and review surface. |
| 57 | + - mavlink-core/src/lib.rs |
0 commit comments