git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
TLTom Levy <tomlevy93@gmail.com>
Date
Jun 17, 2022, 21:27 UTC
Message-ID
<20220617212736.1951-1-tomlevy93@gmail.com>
In-Reply-To
<a3f79b3e034a3a982b3be7835062493bfdf29ee5.1655336146.git.gitgitgadget@gmail.com>
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(-)
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"),
--
2.30.2
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 22 of 38 in “Coverity fixes”
  1. 00/11 Coverity fixesJohannes Schindelin via GitGitGadget, Jun 15, 2022
  2. 01/11 mingw: avoid accessing uninitialized memory in `is_executable()`Johannes Schindelin via GitGitGadget, Jun 15, 2022
  3. Junio C HamanoJun 16, 2022
  4. René ScharfeJun 16, 2022
  5. Junio C HamanoJun 16, 2022
  6. Junio C HamanoJun 16, 2022
  7. 02/11 fsmonitor: avoid memory leak in `fsm_settings__get_incompatible_msg()`Johannes Schindelin via GitGitGadget, Jun 15, 2022
  8. Junio C HamanoJun 16, 2022
  9. 03/11 submodule--helper: avoid memory leak in `update_submodule()`Johannes Schindelin via GitGitGadget, Jun 15, 2022
  10. Junio C HamanoJun 16, 2022
  11. Glen ChooJun 16, 2022
  12. 04/11 get_oid_with_context_1(): avoid use-after-freeJohannes Schindelin via GitGitGadget, Jun 15, 2022
  13. Junio C HamanoJun 16, 2022
  14. 05/11 submodule-config: avoid memory leakJohannes Schindelin via GitGitGadget, Jun 15, 2022
  15. Junio C HamanoJun 16, 2022
  16. Glen ChooJun 16, 2022
  17. 06/11 pack-redundant: avoid using uninitialized memoryJohannes Schindelin via GitGitGadget, Jun 15, 2022
  18. Junio C HamanoJun 16, 2022
  19. 07/11 submodule--helper: avoid memory leak when fetching submodulesJohannes Schindelin via GitGitGadget, Jun 15, 2022
  20. Junio C HamanoJun 16, 2022
  21. 08/11 read_index_from(): avoid memory leakJohannes Schindelin via GitGitGadget, Jun 15, 2022
  22. 08/11 read_index_from(): avoid memory leakTom Levy, Jun 17, 2022
  23. 09/11 pack-mtimes: avoid closing a bogus file descriptorJohannes Schindelin via GitGitGadget, Jun 15, 2022
  24. Taylor BlauJun 16, 2022
  25. 10/11 relative_url(): fix incorrect conditionJohannes Schindelin via GitGitGadget, Jun 15, 2022
  26. Junio C HamanoJun 16, 2022
  27. Ævar Arnfjörð BjarmasonJun 16, 2022
  28. Junio C HamanoJun 16, 2022
  29. Junio C HamanoJun 16, 2022
  30. 11/11 bug_fl(): add missing `va_end()` callJohannes Schindelin via GitGitGadget, Jun 15, 2022
  31. Jeff KingJun 16, 2022
  32. Junio C HamanoJun 16, 2022
  33. Ævar Arnfjörð BjarmasonJun 16, 2022
  34. Junio C HamanoJun 16, 2022
  35. Jeff KingJun 16, 2022
  36. bug_fl(): correctly initialize trace2 va_listJeff King, Jun 16, 2022
  37. Junio C HamanoJun 16, 2022
  38. Junio C HamanoJun 16, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.