From ffafb9cbb106c8e890a9949b49033e6deee44863 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Tue, 11 Mar 2025 16:19:58 +0100 Subject: [PATCH 1/2] fix: avoid assuming struct packing lfs_gstate_t was assumed to be a packed array of uint32_t, but this is not always guaranteed. Access the fields directly instead of attempting to loop over an array of uint32_t Fixes clang tidy warnings about use of uninitialized memory accessed. --- lfs.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lfs.c b/lfs.c index 7520f2ea..901d712e 100644 --- a/lfs.c +++ b/lfs.c @@ -404,16 +404,20 @@ struct lfs_diskoff { // operations on global state static inline void lfs_gstate_xor(lfs_gstate_t *a, const lfs_gstate_t *b) { - for (int i = 0; i < 3; i++) { - ((uint32_t*)a)[i] ^= ((const uint32_t*)b)[i]; - } + a->tag ^= b->tag; + a->pair[0] ^= b->pair[0]; + a->pair[1] ^= b->pair[1]; } static inline bool lfs_gstate_iszero(const lfs_gstate_t *a) { - for (int i = 0; i < 3; i++) { - if (((uint32_t*)a)[i] != 0) { - return false; - } + if (a->tag != 0) { + return false; + } + if (a->pair[0] != 0) { + return false; + } + if (a->pair[1] != 0) { + return false; } return true; } From 61a1b0b496958de3ba1849626296bac8a5a97f02 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 18 Mar 2025 02:39:28 -0500 Subject: [PATCH 2/2] Tweaked lfs_gstate_iszero for terseness --- lfs.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/lfs.c b/lfs.c index 901d712e..d0965a30 100644 --- a/lfs.c +++ b/lfs.c @@ -410,16 +410,9 @@ static inline void lfs_gstate_xor(lfs_gstate_t *a, const lfs_gstate_t *b) { } static inline bool lfs_gstate_iszero(const lfs_gstate_t *a) { - if (a->tag != 0) { - return false; - } - if (a->pair[0] != 0) { - return false; - } - if (a->pair[1] != 0) { - return false; - } - return true; + return a->tag == 0 + && a->pair[0] == 0 + && a->pair[1] == 0; } #ifndef LFS_READONLY