From 7397605517eecfc811a82651027eaffb8783120b Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 4 Feb 2026 17:46:18 -0600 Subject: [PATCH] valgrind: Fixed uninitialized read when truncating fragment leaf Valgrind was reporting a conditional move on uninitialized read here, which is correct. If we fetch a data fragment during a read, the cksize/cksum is meaningless and may be uninitialized. This was somewhat intentional as both lfs3_bptr_claim and LFS3_o_UNCRYST are inconsequential when file->leaf is a data fragment. Why bother checking for a condition that doesn't matter? But keeping Valgrind happy is significantly more important for everyone's mental health. Costs an extra 4 bytes of code: code stack ctx before: 35256 2136 660 after: 35260 (+0.0%) 2136 (+0.0%) 660 (+0.0%) --- lfs3.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lfs3.c b/lfs3.c index 7c9c81c6..423a3183 100644 --- a/lfs3.c +++ b/lfs3.c @@ -14767,9 +14767,10 @@ int lfs3_file_truncate(lfs3_t *lfs3, lfs3_file_t *file, lfs3_off_t size_) { size_ - lfs3_min(file->leaf.pos, size_)); file->leaf.pos = lfs3_min(file->leaf.pos, size_); // mark as crystallized if this truncates our erased-state - if (lfs3_bptr_off(&file->leaf.bptr) - + lfs3_bptr_size(&file->leaf.bptr) - < lfs3_bptr_cksize(&file->leaf.bptr)) { + if (lfs3_bptr_isbptr(&file->leaf.bptr) + && lfs3_bptr_off(&file->leaf.bptr) + + lfs3_bptr_size(&file->leaf.bptr) + < lfs3_bptr_cksize(&file->leaf.bptr)) { lfs3_bptr_claim(&file->leaf.bptr); file->b.h.flags &= ~LFS3_o_UNCRYST; }