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 uses union to set struct superblock size and uses
_Static_assert macro to check the size.
  • Loading branch information
HotMercury committed May 7, 2024
1 parent a59b479 commit c0149bc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
Binary file added mkfs
Binary file not shown.
7 changes: 5 additions & 2 deletions mkfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
#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 */
};
};

/**
Expand Down Expand Up @@ -237,6 +239,7 @@ static int write_data_blocks(int fd, struct superblock *sb)

int main(int argc, char **argv)
{
_Static_assert(sizeof(struct superblock) == SIMPLEFS_BLOCK_SIZE);
if (argc != 2) {
fprintf(stderr, "Usage: %s disk\n", argv[0]);
return EXIT_FAILURE;
Expand Down

0 comments on commit c0149bc

Please sign in to comment.