Thread (162 messages) 162 messages, 2 authors, 2022-09-01
STALE1364d
Revisions (6)
  1. v1 current
  2. v2 [diff vs current]
  3. v3 [diff vs current]
  4. v5 [diff vs current]
  5. v6 [diff vs current]
  6. v7 [diff vs current]

[PATCH 07/11] submodule--helper: fix "errmsg_str" memory leak

From: Ævar Arnfjörð Bjarmason <hidden>
Date: 2022-07-13 13:16:44
Subsystem: the rest · Maintainer: Linus Torvalds

Fix a memory leak introduced in e83e3333b57 (submodule: port submodule
subcommand 'summary' from shell to C, 2020-08-13), to do that stop
juggling around the "errmsg" and "struct strbuf", let's instead move
the "struct strbuf errmsg" to the top-level.

Now we don't need to strbuf_detach() it anymore, but we do need to
ensure that we pass NULL to print_submodule_summary() when we have no
error message.

Signed-off-by: Ævar Arnfjörð Bjarmason <redacted>
---
 builtin/submodule--helper.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index a964dbeec38..a05578a7382 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -932,7 +932,8 @@ static void generate_submodule_summary(struct summary_cb *info,
 {
 	char *displaypath, *src_abbrev = NULL, *dst_abbrev;
 	int missing_src = 0, missing_dst = 0;
-	char *errmsg = NULL;
+	char *errmsg;
+	struct strbuf errmsg_str = STRBUF_INIT;
 	int total_commits = -1;
 
 	if (!info->cached && oideq(&p->oid_dst, null_oid())) {
@@ -1032,7 +1033,6 @@ static void generate_submodule_summary(struct summary_cb *info,
 		 * submodule, i.e., deleted or changed to blob
 		 */
 		if (S_ISGITLINK(p->mod_dst)) {
-			struct strbuf errmsg_str = STRBUF_INIT;
 			if (missing_src && missing_dst) {
 				strbuf_addf(&errmsg_str, "  Warn: %s doesn't contain commits %s and %s\n",
 					    displaypath, oid_to_hex(&p->oid_src),
@@ -1043,10 +1043,10 @@ static void generate_submodule_summary(struct summary_cb *info,
 					    oid_to_hex(&p->oid_src) :
 					    oid_to_hex(&p->oid_dst));
 			}
-			errmsg = strbuf_detach(&errmsg_str, NULL);
 		}
 	}
 
+	errmsg = errmsg_str.len ? errmsg_str.buf : NULL;
 	print_submodule_summary(info, errmsg, total_commits,
 				displaypath, src_abbrev,
 				dst_abbrev, p);
@@ -1054,6 +1054,7 @@ static void generate_submodule_summary(struct summary_cb *info,
 	free(displaypath);
 	free(src_abbrev);
 	free(dst_abbrev);
+	strbuf_release(&errmsg_str);
 }
 
 static void prepare_submodule_summary(struct summary_cb *info,
-- 
2.37.0.932.g7b7031e73bc
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help