Fix gcc14 -Wcalloc-transposed-args warnings
authorCristian Rodríguez <cristian@rodriguez.im>
Sat, 13 Jan 2024 23:14:05 +0000 (20:14 -0300)
committerLuca Boccassi <luca.boccassi@gmail.com>
Sun, 14 Jan 2024 12:57:38 +0000 (12:57 +0000)
all functions annotated with two parameter _alloc_ are calloc-like.
gcc14 enforces this and warns if arguments are backwards.

src/basic/alloc-util.h
src/cryptenroll/cryptenroll.c
src/libsystemd/sd-bus/bus-message.c
src/libsystemd/sd-login/sd-login.c

index 5fbfff0af41663d3d0cf69c5281ff27396a52461..05a6f211f7fc60a57a545056b34440c627b3f19e 100644 (file)
@@ -20,7 +20,7 @@ typedef void* (*mfree_func_t)(void *p);
  * proceeding and smashing the stack limits. Note that by default RLIMIT_STACK is 8M on Linux. */
 #define ALLOCA_MAX (4U*1024U*1024U)
 
-#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
+#define new(t, n) ((t*) malloc_multiply((n), sizeof(t)))
 
 #define new0(t, n) ((t*) calloc((n) ?: 1, sizeof(t)))
 
@@ -45,7 +45,7 @@ typedef void* (*mfree_func_t)(void *p);
                 (t*) alloca0((sizeof(t)*_n_));                          \
         })
 
-#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
+#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)))
 
@@ -112,7 +112,7 @@ static inline bool size_multiply_overflow(size_t size, size_t need) {
         return _unlikely_(need != 0 && size > (SIZE_MAX / need));
 }
 
-_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) {
+_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t need, size_t size) {
         if (size_multiply_overflow(size, need))
                 return NULL;
 
@@ -128,7 +128,7 @@ _alloc_(2, 3) static inline void *reallocarray(void *p, size_t need, size_t size
 }
 #endif
 
-_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t size, size_t need) {
+_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t need, size_t size) {
         if (size_multiply_overflow(size, need))
                 return NULL;
 
index 687b908f060c97f65e108544c322430fa6e69d8c..e1fdc3f5f02448099185f2bd19da9a8fd38c6de1 100644 (file)
@@ -488,7 +488,7 @@ static int parse_argv(int argc, char *argv[]) {
                                         if (n > INT_MAX)
                                                 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Slot index out of range: %u", n);
 
-                                        a = reallocarray(arg_wipe_slots, sizeof(int), arg_n_wipe_slots + 1);
+                                        a = reallocarray(arg_wipe_slots, arg_n_wipe_slots + 1, sizeof(int));
                                         if (!a)
                                                 return log_oom();
 
index 5ac90223ffd88874e2fe1f8da42ef0e71f381758..e3d1eb8f33b7752bcddde0130e92564082fa5fa5 100644 (file)
@@ -1288,7 +1288,7 @@ static int message_push_fd(sd_bus_message *m, int fd) {
         if (copy < 0)
                 return -errno;
 
-        f = reallocarray(m->fds, sizeof(int), m->n_fds + 1);
+        f = reallocarray(m->fds, m->n_fds + 1, sizeof(int));
         if (!f) {
                 m->poisoned = true;
                 safe_close(copy);
index 3e88d349e1689cc37bea398734cc7c0b152fe5b0..c72fccc229e9d30ba1c21074055e117553b2a695 100644 (file)
@@ -1081,7 +1081,7 @@ _public_ int sd_get_uids(uid_t **users) {
                                 uid_t *t;
 
                                 n = MAX(16, 2*r);
-                                t = reallocarray(l, sizeof(uid_t), n);
+                                t = reallocarray(l, n, sizeof(uid_t));
                                 if (!t)
                                         return -ENOMEM;