From 1546a0b59e357985fb0588daf01a8af8ecbd9412 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Wed, 15 Feb 2023 00:30:39 +0100 Subject: [PATCH] fix a memory leak --- tabular.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tabular.c b/tabular.c index 83c1789..8100f04 100644 --- a/tabular.c +++ b/tabular.c @@ -33,7 +33,9 @@ tab_t *resize_tab (tab_t *tab, int nb) tmp = (double *) callocordie (nb, sizeof (double)); memcpy (tmp, tab->data, ((tab->size < nb) ? tab->size : nb) * sizeof (double)); } - free (tab->data); + if (tab->data){ + free (tab->data); + } tab->data = tmp; tab->size = nb; return tab; @@ -134,11 +136,13 @@ double pop_tab (tab_t *tab, int id) if ((!tab) || (((id < 1) || (id > tab->size)) && (id != -1))) { VERBOSE (WARNING, fprintf (stdout, "error out of bounds (%d/%d)\n", id, (tab) ? tab->size : 0)); } else { - ret = tab->data[id - 1]; /* special case for inserting an element at the end */ id = (id == -1) ? tab->size : id; + /* read data */ + ret = tab->data[id - 1]; + /* create larger tab */ double *tmp = (double *) callocordie (tab->size - 1, sizeof (double)); memcpy (tmp, tab->data, (id - 1) * sizeof (double)); -- 2.30.2