From: Tobias Stoeckmann Date: Fri, 13 Sep 2024 17:29:56 +0000 (+0200) Subject: depmod: Replace NOFAIL macro X-Git-Tag: v34~325 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d6d1bebe264e179d2a866617131ee7c9a3fd68e9;p=thirdparty%2Fkmod.git depmod: Replace NOFAIL macro The NOFAIL macro was not implemented. Add a function which gracefully exits depmod instead of letting it crash. Signed-off-by: Tobias Stoeckmann Link: https://github.com/kmod-project/kmod/pull/130 Signed-off-by: Lucas De Marchi --- diff --git a/tools/depmod.c b/tools/depmod.c index 0326a5f0..ab1fa5ad 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -152,6 +152,12 @@ enum node_offset { INDEX_NODE_MASK = 0x0FFFFFFF, /* Offset value */ }; +static void fatal_oom(void) +{ + ERR("out of memory\n"); + exit(EXIT_FAILURE); +} + static struct index_node *index_create(void) { struct index_node *node; @@ -227,7 +233,9 @@ static int index_add_value(struct index_value **values, values = &(*values)->next; len = strlen(value); - v = NOFAIL(calloc(1, sizeof(struct index_value) + len + 1)); + v = calloc(1, sizeof(struct index_value) + len + 1); + if (v == NULL) + fatal_oom(); v->next = *values; v->priority = priority; memcpy(v->value, value, len + 1); @@ -258,9 +266,13 @@ 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(1, sizeof(struct index_node))); + n = calloc(1, sizeof(struct index_node)); + if (n == NULL) + fatal_oom(); memcpy(n, node, sizeof(struct index_node)); - n->prefix = NOFAIL(strdup(&prefix[j+1])); + n->prefix = strdup(&prefix[j+1]); + if (n->prefix == NULL) + fatal_oom(); /* Parent has prefix[0..j], child at prefix[j] */ memset(node, 0, sizeof(struct index_node)); @@ -287,10 +299,14 @@ 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(1, sizeof(struct index_node))); + node->children[ch] = calloc(1, sizeof(struct index_node)); + if (node->children[ch] == NULL) + fatal_oom(); child = node->children[ch]; - child->prefix = NOFAIL(strdup(&key[i+1])); + child->prefix = strdup(&key[i+1]); + if (child->prefix == NULL) + fatal_oom(); child->first = INDEX_CHILDMAX; index_add_value(&child->values, value, priority); @@ -330,7 +346,9 @@ static uint32_t index_write__node(const struct index_node *node, FILE *out) int i; child_count = node->last - node->first + 1; - child_offs = NOFAIL(malloc(child_count * sizeof(uint32_t))); + child_offs = malloc(child_count * sizeof(uint32_t)); + if (child_offs == NULL) + fatal_oom(); for (i = 0; i < child_count; i++) { child = node->children[node->first + i];