Skip to content

Commit

Permalink
Reorder arguments for calloc()-like functions, part #2
Browse files Browse the repository at this point in the history
To appease gcc-14's -Wcalloc-transposed-args check.

Follow-up for 2a9ab09.
  • Loading branch information
mrc0mmand committed Jan 17, 2024
1 parent b551a68 commit fdd8427
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/basic/alloc-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ typedef void* (*mfree_func_t)(void *p);

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

#define newdup_suffix0(t, p, n) ((t*) memdup_suffix0_multiply(p, sizeof(t), (n)))
#define newdup_suffix0(t, p, n) ((t*) memdup_suffix0_multiply(p, (n), sizeof(t)))

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

Expand Down Expand Up @@ -137,7 +137,7 @@ _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t need, si

/* Note that we can't decorate this function with _alloc_() since the returned memory area is one byte larger
* than the product of its parameters. */
static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t need) {
static inline void *memdup_suffix0_multiply(const void *p, size_t need, size_t size) {
if (size_multiply_overflow(size, need))
return NULL;

Expand Down
4 changes: 2 additions & 2 deletions src/boot/efi/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static inline void *xmalloc(size_t size) {
}

_malloc_ _alloc_(1, 2) _returns_nonnull_ _warn_unused_result_
static inline void *xmalloc_multiply(size_t size, size_t n) {
static inline void *xmalloc_multiply(size_t n, size_t size) {
assert_se(!__builtin_mul_overflow(size, n, &size));
return xmalloc(size);
}
Expand All @@ -57,7 +57,7 @@ static inline void* xmemdup(const void *p, size_t l) {
return memcpy(xmalloc(l), p, l);
}

#define xnew(type, n) ((type *) xmalloc_multiply(sizeof(type), (n)))
#define xnew(type, n) ((type *) xmalloc_multiply((n), sizeof(type)))

typedef struct {
EFI_PHYSICAL_ADDRESS addr;
Expand Down
2 changes: 1 addition & 1 deletion src/nspawn/nspawn-bind-user.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ int bind_user_prepare(
if (!sd)
return log_oom();

cm = reallocarray(*custom_mounts, sizeof(CustomMount), *n_custom_mounts + 1);
cm = reallocarray(*custom_mounts, *n_custom_mounts + 1, sizeof(CustomMount));
if (!cm)
return log_oom();

Expand Down
4 changes: 2 additions & 2 deletions src/test/test-alloc-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ TEST(memdup_multiply_and_greedy_realloc) {
size_t i;
int *p;

dup = memdup_suffix0_multiply(org, sizeof(int), 3);
dup = memdup_suffix0_multiply(org, 3, sizeof(int));
assert_se(dup);
assert_se(dup[0] == 1);
assert_se(dup[1] == 2);
assert_se(dup[2] == 3);
assert_se(((uint8_t*) dup)[sizeof(int) * 3] == 0);
free(dup);

dup = memdup_multiply(org, sizeof(int), 3);
dup = memdup_multiply(org, 3, sizeof(int));
assert_se(dup);
assert_se(dup[0] == 1);
assert_se(dup[1] == 2);
Expand Down

0 comments on commit fdd8427

Please sign in to comment.