Skip to content

Commit

Permalink
install: fix -Walloc-size
Browse files Browse the repository at this point in the history
GCC 14 introduces a new -Walloc-size included in -Wextra which gives:
```
src/install/hashmap.c: In function ‘hashmap_new’:
src/install/hashmap.c:83:11: warning: allocation of insufficient size ‘1’ for type ‘Hashmap’ with size ‘40’ [-Walloc-size]
   83 |         h = malloc0(size);
      |           ^
```

malloc0 is a macro deifned by Dracut (and systemd, see below):
```
malloc0(n) (calloc((n), 1))
```

The calloc prototype is:
```
void *calloc(size_t nmemb, size_t size);
```

So, just swap the number of members and size arguments to match the prototype, as
we're initialising 1 struct of size `sizeof(...)`. GCC then sees we're not
doing anything wrong.

This was fixed upstream in systemd in commit f80bb1f7eaf31476a44c2093d3ee02aba817a0b0 [0].

[0] systemd/systemd@f80bb1f

Signed-off-by: Sam James <[email protected]>
  • Loading branch information
thesamesam committed Nov 12, 2023
1 parent 4d59421 commit 6291ac6
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/install/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ bool streq_ptr(const char *a, const char *b);

#define newdup(t, p, n) ((t*) memdup(p, sizeof(t)*(n)))

#define malloc0(n) (calloc((n), 1))
#define malloc0(n) (calloc(1, (n)))

static inline const char *yes_no(bool b)
{
Expand Down

0 comments on commit 6291ac6

Please sign in to comment.