Skip to content

Commit ffafb9c

Browse files
committed
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.
1 parent 0494ce7 commit ffafb9c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

lfs.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,16 +404,20 @@ struct lfs_diskoff {
404404

405405
// operations on global state
406406
static inline void lfs_gstate_xor(lfs_gstate_t *a, const lfs_gstate_t *b) {
407-
for (int i = 0; i < 3; i++) {
408-
((uint32_t*)a)[i] ^= ((const uint32_t*)b)[i];
409-
}
407+
a->tag ^= b->tag;
408+
a->pair[0] ^= b->pair[0];
409+
a->pair[1] ^= b->pair[1];
410410
}
411411

412412
static inline bool lfs_gstate_iszero(const lfs_gstate_t *a) {
413-
for (int i = 0; i < 3; i++) {
414-
if (((uint32_t*)a)[i] != 0) {
415-
return false;
416-
}
413+
if (a->tag != 0) {
414+
return false;
415+
}
416+
if (a->pair[0] != 0) {
417+
return false;
418+
}
419+
if (a->pair[1] != 0) {
420+
return false;
417421
}
418422
return true;
419423
}

0 commit comments

Comments
 (0)