From: Matthew Fernandez Date: Sat, 5 Nov 2022 15:54:38 +0000 (-0700) Subject: prune: replace allocations with cgraph wrappers X-Git-Tag: 7.0.2~17^2~11 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f8f7e0a9e570e06978410f8e6e81d33943afd5f7;p=graphviz prune: replace allocations with cgraph wrappers This application has no ability to handle allocation failures. For example, allocation failure during adding an attribute would result in setting `attr_list` to null which would then go on to crash the program. Lets stop pretending `prune` handles these cases and just exit on allocation failure. --- diff --git a/contrib/prune/generic_list.c b/contrib/prune/generic_list.c index afe464fc0..5484eb362 100644 --- a/contrib/prune/generic_list.c +++ b/contrib/prune/generic_list.c @@ -8,11 +8,9 @@ * Contributors: Details at http://www.graphviz.org/ *************************************************************************/ - -#include +#include #include #include -#include #include "generic_list.h" @@ -20,24 +18,11 @@ generic_list_t *new_generic_list(uint64_t size) { - generic_list_t *list; - - list = malloc(sizeof(generic_list_t)); - if (list == NULL) { - perror("[new_generic_list()] Error allocating memory:"); - return NULL; - } + generic_list_t *list = gv_alloc(sizeof(generic_list_t)); if (size != 0) { - list->data = malloc(size * sizeof(gl_data)); - if (list->data == NULL) { - perror("[new_generic_list()] Error allocating memory:"); - free(list); - return NULL; - } - } else - list->data = NULL; + list->data = gv_calloc(size, sizeof(gl_data)); + } list->size = size; - list->used = 0; return list; } @@ -52,7 +37,6 @@ void free_generic_list(generic_list_t * list) generic_list_t *add_to_generic_list(generic_list_t * list, gl_data element) { uint64_t new_size; - gl_data *new_data; if (list->size == list->used) { if (list->size == 0) { @@ -60,11 +44,8 @@ generic_list_t *add_to_generic_list(generic_list_t * list, gl_data element) } else { new_size = list->size * 2; } - new_data = realloc(list->data, new_size * sizeof(gl_data)); - if (new_data == NULL) { - perror("[add_to_generic_list()] Error allocating memory:"); - return NULL; - } + gl_data *new_data = gv_recalloc(list->data, list->size, new_size, + sizeof(gl_data)); list->data = new_data; list->size = new_size; } diff --git a/contrib/prune/prune.c b/contrib/prune/prune.c index 3f9fca402..b5a8e28ca 100644 --- a/contrib/prune/prune.c +++ b/contrib/prune/prune.c @@ -14,7 +14,7 @@ #include #include - +#include #include #include #include @@ -254,13 +254,8 @@ Agraph_t *gread(FILE * fp) generic_list_t *addattr(generic_list_t * l, char *a) { char *p; - strattr_t *sp; - sp = malloc(sizeof(strattr_t)); - if (sp == NULL) { - perror("[addattr()->malloc()]"); - graphviz_exit(EXIT_FAILURE); - } + strattr_t *sp = gv_alloc(sizeof(strattr_t)); /* Split argument spec. at first '=' */ p = strchr(a, '='); @@ -271,18 +266,10 @@ generic_list_t *addattr(generic_list_t * l, char *a) *(p++) = '\0'; /* pointer to argument name */ - sp->n = strdup(a); - if (sp->n == NULL) { - perror("[addattr()->strdup()]"); - graphviz_exit(EXIT_FAILURE); - } + sp->n = gv_strdup(a); /* pointer to argument value */ - sp->v = strdup(p); - if (sp->v == NULL) { - perror("[addattr()->strdup()]"); - graphviz_exit(EXIT_FAILURE); - } + sp->v = gv_strdup(p); return add_to_generic_list(l, (gl_data) sp); } @@ -290,13 +277,7 @@ generic_list_t *addattr(generic_list_t * l, char *a) /* add element to node list */ generic_list_t *addnode(generic_list_t * l, char *n) { - char *sp; - - sp = strdup(n); - if (sp == NULL) { - perror("[addnode()->strdup()]"); - graphviz_exit(EXIT_FAILURE); - } + char *sp = gv_strdup(n); return add_to_generic_list(l, (gl_data) sp); }