Skip to content

Commit

Permalink
Ensure superblock always fits SIMPLEFS_BLOCK_SIZE
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
HotMercury committed May 7, 2024
1 parent a59b479 commit 056bd2a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions mkfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
#include "simplefs.h"

struct superblock {
struct simplefs_sb_info info;
char padding[4064]; /* Padding to match block size */
union {
struct simplefs_sb_info info;
char padding[SIMPLEFS_BLOCK_SIZE]; /* Padding to match block size */
};
};

_Static_assert(sizeof(struct superblock) == SIMPLEFS_BLOCK_SIZE);

/**
* DIV_ROUND_UP - round up a division
* @n: dividend
Expand Down

0 comments on commit 056bd2a

Please sign in to comment.