Skip to content

Commit 4fa2687

Browse files
ahuntgitster
authored andcommitted
builtin/bugreport: don't leak prefixed filename
prefix_filename() returns newly allocated memory, and strbuf_addstr() doesn't take ownership of its inputs. Therefore we have to make sure to store and free prefix_filename()'s result. As this leak is in cmd_bugreport(), we could just as well UNLEAK the prefix - but there's no good reason not to just free it properly. This leak was found while running t0091, see output below: Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x49ab79 in realloc /home/abuild/rpmbuild/BUILD/llvm-11.0.0.src/build/../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0x9acc66 in xrealloc wrapper.c:126:8 #2 0x93baed in strbuf_grow strbuf.c:98:2 #3 0x93c6ea in strbuf_add strbuf.c:295:2 #4 0x69f162 in strbuf_addstr ./strbuf.h:304:2 #5 0x69f083 in prefix_filename abspath.c:277:2 #6 0x4fb275 in cmd_bugreport builtin/bugreport.c:146:9 #7 0x4cd91d in run_builtin git.c:467:11 #8 0x4cb5f3 in handle_builtin git.c:719:3 #9 0x4ccf47 in run_argv git.c:808:4 #10 0x4caf49 in cmd_main git.c:939:19 #11 0x69df9e in main common-main.c:52:11 #12 0x7f523a987349 in __libc_start_main (/lib64/libc.so.6+0x24349) Signed-off-by: Andrzej Hunt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d895804 commit 4fa2687

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

builtin/bugreport.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
129129
char *option_output = NULL;
130130
char *option_suffix = "%Y-%m-%d-%H%M";
131131
const char *user_relative_path = NULL;
132+
char *prefixed_filename;
132133

133134
const struct option bugreport_options[] = {
134135
OPT_STRING('o', "output-directory", &option_output, N_("path"),
@@ -142,9 +143,9 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
142143
bugreport_usage, 0);
143144

144145
/* Prepare the path to put the result */
145-
strbuf_addstr(&report_path,
146-
prefix_filename(prefix,
147-
option_output ? option_output : ""));
146+
prefixed_filename = prefix_filename(prefix,
147+
option_output ? option_output : "");
148+
strbuf_addstr(&report_path, prefixed_filename);
148149
strbuf_complete(&report_path, '/');
149150

150151
strbuf_addstr(&report_path, "git-bugreport-");
@@ -189,6 +190,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
189190
fprintf(stderr, _("Created new report at '%s'.\n"),
190191
user_relative_path);
191192

193+
free(prefixed_filename);
192194
UNLEAK(buffer);
193195
UNLEAK(report_path);
194196
return !!launch_editor(report_path.buf, NULL, NULL);

0 commit comments

Comments
 (0)