Skip to content

Commit

Permalink
daemon/mmapped: use static_assert on undefined padding
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukáš Ondráček authored and vcunat committed Nov 4, 2024
1 parent 555afe8 commit 53db552
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
11 changes: 6 additions & 5 deletions daemon/defer.c
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,12 @@ int defer_init(const char *mmap_file, int cpus)

size_t size = offsetof(struct defer, kru) + KRU.get_size(capacity_log);
size_t header_size = offsetof(struct defer, using_avx2) + sizeof(header.using_avx2);
kr_assert(header_size ==
sizeof(header.capacity) +
sizeof(header.max_decay) +
sizeof(header.cpus) +
sizeof(header.using_avx2)); // no undefined padding inside
static_assert( // no padding up to .using_avx2
offsetof(struct defer, using_avx2) ==
sizeof(header.capacity) +
sizeof(header.max_decay) +
sizeof(header.cpus),
"detected padding with undefined data inside mmapped header");

ret = mmapped_init(&defer_mmapped, mmap_file, size, &header, header_size);
if (ret == MMAPPED_WAS_FIRST) {
Expand Down
17 changes: 9 additions & 8 deletions daemon/ratelimiting.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ int ratelimiting_init(const char *mmap_file, size_t capacity, uint32_t instant_l
};

size_t header_size = offsetof(struct ratelimiting, using_avx2) + sizeof(header.using_avx2);
kr_assert(header_size ==
sizeof(header.capacity) +
sizeof(header.instant_limit) +
sizeof(header.rate_limit) +
sizeof(header.log_period) +
sizeof(header.slip) +
sizeof(header.dry_run) +
sizeof(header.using_avx2)); // no undefined padding inside
static_assert( // no padding up to .using_avx2
offsetof(struct ratelimiting, using_avx2) ==
sizeof(header.capacity) +
sizeof(header.instant_limit) +
sizeof(header.rate_limit) +
sizeof(header.log_period) +
sizeof(header.slip) +
sizeof(header.dry_run),
"detected padding with undefined data inside mmapped header");

int ret = mmapped_init(&ratelimiting_mmapped, mmap_file, size, &header, header_size);
if (ret == MMAPPED_WAS_FIRST) {
Expand Down

0 comments on commit 53db552

Please sign in to comment.