From 45718fc257ec5ebd2feb9187e01f9afcb63926b6 Mon Sep 17 00:00:00 2001 From: Aleksander Date: Tue, 26 Dec 2023 04:55:50 +0100 Subject: [PATCH 1/2] changed the way malloc is handled in options.c --- src/alloclist.c | 47 ++++++++++++++++++++++++++++++ src/alloclist.h | 22 ++++++++++++++ src/options.c | 76 ++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 src/alloclist.c create mode 100644 src/alloclist.h diff --git a/src/alloclist.c b/src/alloclist.c new file mode 100644 index 000000000..23d4270ff --- /dev/null +++ b/src/alloclist.c @@ -0,0 +1,47 @@ +#include +#include +#include "alloclist.h" + +// initialize allocation list +void alloc_list_init(alloc_list *list){ + list->head = NULL; +} + +// try to add a new allocation node to list +int alloc_list_add(alloc_list *list, void *data){ + alloc_node *new_node = NULL; + if((new_node=(alloc_node*)malloc(sizeof(alloc_node)))==NULL){ + return -1; + } + new_node->data = data; + new_node->next = list->head; + list->head = new_node; + return 0; +} + +// remove the first element from allocation list +int alloc_list_pop(alloc_list *list, void **data){ + if(list->head==NULL){ + return -1; + } + alloc_node *old_node = list->head; + list->head = list->head->next; + old_node->next = NULL; + *data = old_node->data; + free(old_node); + return 0; +} + +// destroy only the allocation list nodes without freeing what they point to +void alloc_list_destroy_clean(alloc_list *list){ + void *data = NULL; + while(alloc_list_pop(list,&data)==0){} +} + +// destroy the whole allocation list and free all elements it points to +void alloc_list_destroy(alloc_list *list){ + void *data = NULL; + while(alloc_list_pop(list,&data)==0){ + free(data); + } +} \ No newline at end of file diff --git a/src/alloclist.h b/src/alloclist.h new file mode 100644 index 000000000..8ddef6913 --- /dev/null +++ b/src/alloclist.h @@ -0,0 +1,22 @@ +#ifndef ALLOC_LIST_H +#define ALLOC_LIST_H + +struct alloc_node{ + void *data; + struct alloc_node *next; +}; + +struct alloc_list{ + struct alloc_node *head; +}; + +typedef struct alloc_node alloc_node; +typedef struct alloc_list alloc_list; + +void alloc_list_init(alloc_list *list); +int alloc_list_add(alloc_list *list, void *data); +int alloc_list_pop(alloc_list *list, void **data); +void alloc_list_destroy_clean(alloc_list *list); +void alloc_list_destroy(alloc_list *list); + +#endif \ No newline at end of file diff --git a/src/options.c b/src/options.c index 2145b33bb..4ec8034df 100644 --- a/src/options.c +++ b/src/options.c @@ -15,6 +15,7 @@ #include "options.h" #include "print.h" #include "util.h" +#include "alloclist.h" const char *color_line_number = "\033[1;33m"; /* bold yellow */ const char *color_match = "\033[30;43m"; /* black with yellow background */ @@ -234,8 +235,10 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) { size_t *ext_index = NULL; char *extensions = NULL; size_t num_exts = 0; + alloc_list alist; init_options(); + alloc_list_init(&alist); option_t base_longopts[] = { { "ackmate", no_argument, &opts.ackmate, 1 }, @@ -337,9 +340,29 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) { lang_count = get_lang_count(); longopts_len = (sizeof(base_longopts) / sizeof(option_t)); full_len = (longopts_len + lang_count + 1); - longopts = ag_malloc(full_len * sizeof(option_t)); + + longopts = (option_t*)malloc(full_len * sizeof(option_t)); + if(longopts==NULL){ + alloc_list_destroy(&alist); + die("Memory allocation failed."); + } + if(alloc_list_add(&alist,longopts)!=0){ + alloc_list_destroy(&alist); + die("Memory allocation failed."); + } + memcpy(longopts, base_longopts, sizeof(base_longopts)); - ext_index = (size_t *)ag_malloc(sizeof(size_t) * lang_count); + + ext_index = (size_t*)malloc(sizeof(size_t) * lang_count); + if(ext_index==NULL){ + alloc_list_destroy(&alist); + die("Memory allocation failed."); + } + if(alloc_list_add(&alist,ext_index)!=0){ + alloc_list_destroy(&alist); + die("Memory allocation failed."); + } + memset(ext_index, 0, sizeof(size_t) * lang_count); for (i = 0; i < lang_count; i++) { @@ -818,7 +841,17 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) { } (*paths)[i] = path; #ifdef PATH_MAX - tmp = ag_malloc(PATH_MAX); + + tmp = malloc(PATH_MAX); + if(tmp==NULL){ + alloc_list_destroy(&alist); + die("Memory allocation failed."); + } + if(alloc_list_add(&alist,tmp)!=0){ + alloc_list_destroy(&alist); + die("Memory allocation failed."); + } + base_path = realpath(path, tmp); #else base_path = realpath(path, NULL); @@ -838,11 +871,39 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) { opts.search_stream = 0; } else { path = ag_strdup("."); - *paths = ag_malloc(sizeof(char *) * 2); - *base_paths = ag_malloc(sizeof(char *) * 2); + + *paths = malloc(sizeof(char *) * 2); + if(*paths==NULL){ + alloc_list_destroy(&alist); + die("Memory allocation failed."); + } + if(alloc_list_add(&alist,*paths)!=0){ + alloc_list_destroy(&alist); + die("Memory allocation failed."); + } + + *base_paths = malloc(sizeof(char *) * 2); + if(*base_paths==NULL){ + alloc_list_destroy(&alist); + die("Memory allocation failed."); + } + if(alloc_list_add(&alist,*base_paths)!=0){ + alloc_list_destroy(&alist); + die("Memory allocation failed."); + } + (*paths)[0] = path; #ifdef PATH_MAX - tmp = ag_malloc(PATH_MAX); + tmp = malloc(PATH_MAX); + if(tmp==NULL){ + alloc_list_destroy(&alist); + die("Memory allocation failed."); + } + if(alloc_list_add(&alist,tmp)!=0){ + alloc_list_destroy(&alist); + die("Memory allocation failed."); + } + (*base_paths)[0] = realpath(path, tmp); #else (*base_paths)[0] = realpath(path, NULL); @@ -855,4 +916,5 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) { #ifdef _WIN32 windows_use_ansi(opts.color_win_ansi); #endif -} + alloc_list_destroy_clean(&alist); +} \ No newline at end of file From 7ace0470b644a846c9e8d298e977b25a876c21f6 Mon Sep 17 00:00:00 2001 From: Aleksander Date: Tue, 26 Dec 2023 05:00:47 +0100 Subject: [PATCH 2/2] changed mallocking in options.c --- Makefile.am | 2 +- Makefile.w32 | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile.am b/Makefile.am index 3931c3a71..1e3836707 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,7 @@ ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} bin_PROGRAMS = ag -ag_SOURCES = src/ignore.c src/ignore.h src/log.c src/log.h src/options.c src/options.h src/print.c src/print_w32.c src/print.h src/scandir.c src/scandir.h src/search.c src/search.h src/lang.c src/lang.h src/util.c src/util.h src/decompress.c src/decompress.h src/uthash.h src/main.c src/zfile.c +ag_SOURCES = src/alloclist.c src/ignore.c src/ignore.h src/log.c src/log.h src/options.c src/options.h src/print.c src/print_w32.c src/print.h src/scandir.c src/scandir.h src/search.c src/search.h src/lang.c src/lang.h src/util.c src/util.h src/decompress.c src/decompress.h src/uthash.h src/main.c src/zfile.c ag_LDADD = ${PCRE_LIBS} ${LZMA_LIBS} ${ZLIB_LIBS} $(PTHREAD_LIBS) dist_man_MANS = doc/ag.1 diff --git a/Makefile.w32 b/Makefile.w32 index 279465185..ce15ee865 100644 --- a/Makefile.w32 +++ b/Makefile.w32 @@ -15,6 +15,7 @@ SRCS = \ src/scandir.c \ src/search.c \ src/util.c \ + src/alloclist.c \ src/print_w32.c OBJS = $(subst .c,.o,$(SRCS))