Skip to content

Commit 056bd2a

Browse files
committed
Ensure superblock always fits SIMPLEFS_BLOCK_SIZE
In current implementation, the struct superblock used padding via subtraction to match the SIMPLEFS_BLOCK_SIZE, which could lead to the need for recalculating the padding when the data structure changed. This commit introduces a union that allocates the largest size among its members to set the struct superblock size, ensuring that it is at least SIMPLEFS_BLOCK_SIZE. Additionally, it utilizes the _Static_assert keyword to verify whether the size matches SIMPLEFS_BLOCK_SIZE.
1 parent a59b479 commit 056bd2a

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

mkfs.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
#include "simplefs.h"
1212

1313
struct superblock {
14-
struct simplefs_sb_info info;
15-
char padding[4064]; /* Padding to match block size */
14+
union {
15+
struct simplefs_sb_info info;
16+
char padding[SIMPLEFS_BLOCK_SIZE]; /* Padding to match block size */
17+
};
1618
};
1719

20+
_Static_assert(sizeof(struct superblock) == SIMPLEFS_BLOCK_SIZE);
21+
1822
/**
1923
* DIV_ROUND_UP - round up a division
2024
* @n: dividend

0 commit comments

Comments
 (0)