]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
tools: depmod: fix -Walloc-size
authorSam James <sam@gentoo.org>
Sun, 5 Nov 2023 22:02:25 +0000 (22:02 +0000)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Mon, 6 Nov 2023 00:22:05 +0000 (18:22 -0600)
GCC 14 introduces a new -Walloc-size included in -Wextra which gives:
```
tools/depmod.c:192:14: warning: allocation of insufficient size ‘1’ for type ‘struct index_node’ with size ‘1048’ [-Walloc-size]
tools/depmod.c:255:11: warning: allocation of insufficient size ‘1’ for type ‘struct index_value’ with size ‘16’ [-Walloc-size]
tools/depmod.c:286:35: warning: allocation of insufficient size ‘1’ for type ‘struct index_node’ with size ‘1048’ [-Walloc-size]
tools/depmod.c:315:44: warning: allocation of insufficient size ‘1’ for type ‘struct index_node’ with size ‘1048’ [-Walloc-size]
```

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(struct ...)`. GCC then sees we're not
doing anything wrong.

Signed-off-by: Sam James <sam@gentoo.org>
tools/depmod.c

index 630fef9c8fb0fa1a03d80e9122f33b80a9c0ea3c..ab8513b21526aeee2ab8020abe9cd162e54ec5e1 100644 (file)
@@ -190,7 +190,7 @@ static struct index_node *index_create(void)
 {
        struct index_node *node;
 
-       node = NOFAIL(calloc(sizeof(struct index_node), 1));
+       node = NOFAIL(calloc(1, sizeof(struct index_node)));
        node->prefix = NOFAIL(strdup(""));
        node->first = INDEX_CHILDMAX;
 
@@ -253,7 +253,7 @@ static int index_add_value(struct index_value **values,
                values = &(*values)->next;
 
        len = strlen(value);
-       v = NOFAIL(calloc(sizeof(struct index_value) + len + 1, 1));
+       v = NOFAIL(calloc(1, sizeof(struct index_value) + len + 1));
        v->next = *values;
        v->priority = priority;
        memcpy(v->value, value, len + 1);
@@ -284,7 +284,7 @@ static int index_insert(struct index_node *node, const char *key,
                                struct index_node *n;
 
                                /* New child is copy of node with prefix[j+1..N] */
-                               n = NOFAIL(calloc(sizeof(struct index_node), 1));
+                               n = NOFAIL(calloc(1, sizeof(struct index_node)));
                                memcpy(n, node, sizeof(struct index_node));
                                n->prefix = NOFAIL(strdup(&prefix[j+1]));
 
@@ -313,7 +313,7 @@ static int index_insert(struct index_node *node, const char *key,
                                node->first = ch;
                        if (ch > node->last)
                                node->last = ch;
-                       node->children[ch] = NOFAIL(calloc(sizeof(struct index_node), 1));
+                       node->children[ch] = NOFAIL(calloc(1, sizeof(struct index_node)));
 
                        child = node->children[ch];
                        child->prefix = NOFAIL(strdup(&key[i+1]));