[pacman-dev] [PATCH 1/3] pacsort: handle failing list_add
Since it can fail, check the return value. If it fails, we need to free the memory of the object we wanted to add to the list. Signed-off-by: Rikard Falkeborn <rikard.falkeborn@gmail.com> --- src/util/pacsort.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/util/pacsort.c b/src/util/pacsort.c index b0137ec..003ec07 100644 --- a/src/util/pacsort.c +++ b/src/util/pacsort.c @@ -254,7 +254,10 @@ static char *explode(struct buffer_t *buffer, struct list_t *list) while((end = memchr(ptr, linedelim, &buffer->mem[buffer->len] - ptr))) { *end = '\0'; meta = input_new(ptr, end - ptr); - list_add(list, meta); + if(meta == NULL || list_add(list, meta) != 0) { + input_free(meta); + return NULL; + } ptr = end + 1; } @@ -294,6 +297,7 @@ static int splitfile(FILE *stream, struct buffer_t *buffer, struct list_t *list) if(buffer->len) { struct input_t *meta = input_new(buffer->mem, buffer->len + 1); if(meta == NULL || list_add(list, meta) != 0) { + input_free(meta); return 1; } } -- 2.5.0
That makes it impossible to free it later. Signed-off-by: Rikard Falkeborn <rikard.falkeborn@gmail.com> --- src/util/pacsort.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/util/pacsort.c b/src/util/pacsort.c index 003ec07..3337d97 100644 --- a/src/util/pacsort.c +++ b/src/util/pacsort.c @@ -104,10 +104,11 @@ static void buffer_free(struct buffer_t *buf) static int buffer_grow(struct buffer_t *buffer) { size_t newsz = buffer->maxlen * 2.5; - buffer->mem = realloc(buffer->mem, newsz * sizeof(char)); - if(!buffer->mem) { + char* new_mem = realloc(buffer->mem, newsz * sizeof(char)); + if(!new_mem) { return 1; } + buffer->mem = new_mem; buffer->maxlen = newsz; return 0; @@ -136,11 +137,12 @@ static struct list_t *list_new(size_t initial_size) static int list_grow(struct list_t *list) { size_t newsz = list->maxcount * 2.5; - list->list = realloc(list->list, newsz * sizeof(char *)); - if(!list->list) { + void **new_list = realloc(list->list, newsz * sizeof(char *)); + if(!new_list) { return 1; } + list->list = new_list; list->maxcount = newsz; return 0; -- 2.5.0
On 11/08/15 05:42, Rikard Falkeborn wrote:
That makes it impossible to free it later.
Signed-off-by: Rikard Falkeborn <rikard.falkeborn@gmail.com>
OK.
* free memory * close open file Signed-off-by: Rikard Falkeborn <rikard.falkeborn@gmail.com> --- src/util/pacsort.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/util/pacsort.c b/src/util/pacsort.c index 3337d97..e7dc63e 100644 --- a/src/util/pacsort.c +++ b/src/util/pacsort.c @@ -484,6 +484,7 @@ int main(int argc, char *argv[]) struct list_t *list; struct buffer_t *buffer; size_t i; + int ret = 0; /* option defaults */ opts.order = 1; @@ -507,7 +508,8 @@ int main(int argc, char *argv[]) if(optind == argc) { if(splitfile(stdin, buffer, list) != 0) { fprintf(stderr, "%s: memory exhausted\n", argv[0]); - return ENOMEM; + ret = ENOMEM; + goto cleanup; } } else { while(optind < argc) { @@ -515,7 +517,9 @@ int main(int argc, char *argv[]) if(input) { if(splitfile(input, buffer, list) != 0) { fprintf(stderr, "%s: memory exhausted\n", argv[0]); - return ENOMEM; + fclose(input); + ret = ENOMEM; + goto cleanup; } fclose(input); } else { @@ -534,10 +538,11 @@ int main(int argc, char *argv[]) } } +cleanup: list_free(list, input_free); buffer_free(buffer); - return 0; + return ret; } /* vim: set noet: */ -- 2.5.0
On 11/08/15 05:42, Rikard Falkeborn wrote:
* free memory * close open file
Signed-off-by: Rikard Falkeborn <rikard.falkeborn@gmail.com>
OK.
On 11/08/15 05:42, Rikard Falkeborn wrote:
Since it can fail, check the return value. If it fails, we need to free the memory of the object we wanted to add to the list.
Signed-off-by: Rikard Falkeborn <rikard.falkeborn@gmail.com>
OK. Sorry for the long delay! (Two months is not that bad...)
participants (2)
-
Allan McRae
-
Rikard Falkeborn