Skip to content

fix(storage): correctness bugs in ModelMap::writeModelLabels - #7713

Merged
pfeerick merged 2 commits into
mainfrom
fix/writeModelLabels-file-corruption
Aug 26, 2026
Merged

fix(storage): correctness bugs in ModelMap::writeModelLabels#7713
pfeerick merged 2 commits into
mainfrom
fix/writeModelLabels-file-corruption

Conversation

@pfeerick

Copy link
Copy Markdown
Member

Summary

Found while independently reviewing the raw-file label rewrite added by #7709 (ModelMap::writeModelLabels()), which reads a model's existing .yml, rewrites its header: section, and raw-copies the rest of the file byte-for-byte into a temp file before swapping it in.

Three issues in that copy path:

  • Uninitialized stack memory written to disk. The header search/skip bounds used sizeof(buf) (512) instead of the number of bytes actually read. For any model file shorter than 512 bytes, the remainder of the stack buffer is uninitialized, and that garbage got written into the rewritten model file.
  • Inverted return-value polarity. writeModelLabels() returns true on success (per its own docstring), but both callers (renameLabel, updateModelFile) assign the result directly to a variable named fault, whose convention elsewhere in those same functions is true = failure. A successful rename would report failure and vice versa. Currently latent since both real call sites discard the return value, but wrong as written and a landmine for the next caller that checks it.
  • Unchecked short write / rename. Neither f_write()'s actual bytes-written count nor f_rename()'s result were checked, so a full or removed SD card mid-rename could silently delete the original file and leave a truncated replacement in its place.

Test plan

Added two tests to radio/src/tests/modelslist.cpp (ModelsList.WriteModelLabelsPreservesBodySize, ModelsList.WriteModelLabelsFailsCleanlyWithoutHeader), exercising writeModelLabels() against a real (simulated) FatFS filesystem. Confirmed the first test fails against the pre-fix code (both outputs collapse to the same size regardless of real body-length difference) and passes against the fix.

  • gtests-radio passes on a STORAGE_MODELSLIST target (PCB=X10) - 110/110, including the 2 new tests
  • gtests-radio passes on a B&W target (PCB=X9D+) - 122/122 (new tests compile out there, since ModelMap doesn't exist on B&W)

🤖 Generated with Claude Code

pfeerick and others added 2 commits August 26, 2026 22:08
Three issues found while reviewing #7709's raw-file label rewrite:

- Header search/copy bounds used sizeof(buf) instead of the actual
  bytes read, so a model file shorter than 512 bytes had uninitialized
  stack memory written into the rewritten file.
- writeModelLabels() returns true on success, but both callers
  (renameLabel, updateModelFile) assigned it directly to a variable
  named 'fault', inverting the polarity - a successful rename reported
  failure and vice versa. Currently latent since both call sites
  discard the return value, but wrong as written.
- f_write() and f_rename() results weren't checked for short
  writes/failure, so a full or removed SD card during a rename could
  silently delete the original file and leave a truncated replacement.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
WriteModelLabelsPreservesBodySize pins the uninitialized-memory fix: two
model files under 512 bytes differing only in body length must produce
outputs whose size differs by exactly that same amount. Verified this
fails against the pre-fix code (both outputs collapsed to the same size,
padded with garbage instead of bounded by the real byte count) and
passes against the fix.

WriteModelLabelsFailsCleanlyWithoutHeader covers the missing-header
error path.

Requires SIMU + STORAGE_MODELSLIST (a color-target native build, e.g.
PCB=X10) - ModelMap/writeModelLabels don't exist on B&W targets.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
@pfeerick pfeerick added this to the 2.12.3 milestone Aug 26, 2026
@pfeerick pfeerick added bug/regression ↩️ A new version of EdgeTX broke something unit tests 🧪 backport/2.12 To be backported to a 2.12 release also. labels Aug 26, 2026
@pfeerick
pfeerick marked this pull request as ready for review August 26, 2026 23:09
@pfeerick
pfeerick merged commit 3562502 into main Aug 26, 2026
45 checks passed
@pfeerick
pfeerick deleted the fix/writeModelLabels-file-corruption branch August 26, 2026 23:13
pfeerick added a commit that referenced this pull request Aug 26, 2026
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
(cherry picked from commit 3562502)
@pfeerick pfeerick mentioned this pull request Aug 26, 2026
12 tasks
pfeerick added a commit that referenced this pull request Aug 28, 2026
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
(cherry picked from commit 3562502)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/2.12 To be backported to a 2.12 release also. bug/regression ↩️ A new version of EdgeTX broke something unit tests 🧪

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant