diff mbox series

[08/11] read_index_from(): avoid memory leak

Message ID 20220617212736.1951-1-tomlevy93@gmail.com (mailing list archive)
State New, archived
Headers show
Series None | expand

Commit Message

Tom Levy June 17, 2022, 9:27 p.m. UTC
Hi,

I think there is another problem with this code: later on it refers to
the 'base_path' variable, but I think it should refer to 'base_path2'
if the first path did not exist.

How about the following patch instead (or in addition)?

Regards,
Tom Levy


-- >8 --

Subject: [PATCH] read_index_from(): use second path consistently, avoid memory
 leak

In 998330ac2e7c (read-cache: look for shared index files next to the
index, too, 2021-08-26), we added code that allocates memory to store
the path of a second shared base index, but we never released that
memory. Also, later code (e.g. the call to freshen_shared_index())
continued to use the first path even when the second path was
selected.

Modify the code to store the second path in the original variable
(after freeing the first path), so that the later code will use the
second path (and also free it).

The memory leak was reported by Coverity.

Co-authored-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Tom Levy <tomlevy93@gmail.com>
---
 read-cache.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

--
2.30.2
diff mbox series

Patch

diff --git a/read-cache.c b/read-cache.c
index 4df97e185e..48f2b9aa9d 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2459,15 +2459,15 @@  int read_index_from(struct index_state *istate, const char *path,
                                   the_repository, "%s", base_path);
        if (!ret) {
                char *path_copy = xstrdup(path);
-               const char *base_path2 = xstrfmt("%s/sharedindex.%s",
-                                                dirname(path_copy),
-                                                base_oid_hex);
+               free(base_path);
+               base_path = xstrfmt("%s/sharedindex.%s", dirname(path_copy),
+                                   base_oid_hex);
                free(path_copy);
                trace2_region_enter_printf("index", "shared/do_read_index",
-                                          the_repository, "%s", base_path2);
-               ret = do_read_index(split_index->base, base_path2, 1);
+                                          the_repository, "%s", base_path);
+               ret = do_read_index(split_index->base, base_path, 1);
                trace2_region_leave_printf("index", "shared/do_read_index",
-                                          the_repository, "%s", base_path2);
+                                          the_repository, "%s", base_path);
        }
        if (!oideq(&split_index->base_oid, &split_index->base->oid))
                die(_("broken index, expect %s in %s, got %s"),