Skip to content

Commit

Permalink
builtin/rm: avoid leaking pathspec and seen
Browse files Browse the repository at this point in the history
parse_pathspec() populates pathspec, hence we need to clear it once it's
no longer needed. seen is xcalloc'd within the same function and
likewise needs to be freed once its no longer needed.

cmd_rm() has multiple early returns, therefore we need to clear or free
as soon as this data is no longer needed, as opposed to doing a cleanup
at the end.

LSAN output from t0020:

Direct leak of 112 byte(s) in 1 object(s) allocated from:
    #0 0x49a85d in malloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3
    microsoft#1 0x9ac0a4 in do_xmalloc wrapper.c:41:8
    microsoft#2 0x9ac07a in xmalloc wrapper.c:62:9
    microsoft#3 0x873277 in parse_pathspec pathspec.c:582:2
    microsoft#4 0x646ffa in cmd_rm builtin/rm.c:266:2
    microsoft#5 0x4cd91d in run_builtin git.c:467:11
    microsoft#6 0x4cb5f3 in handle_builtin git.c:719:3
    microsoft#7 0x4ccf47 in run_argv git.c:808:4
    microsoft#8 0x4caf49 in cmd_main git.c:939:19
    microsoft#9 0x69dc0e in main common-main.c:52:11
    microsoft#10 0x7f948825b349 in __libc_start_main (/lib64/libc.so.6+0x24349)

Indirect leak of 65 byte(s) in 1 object(s) allocated from:
    #0 0x49ab79 in realloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3
    microsoft#1 0x9ac2a6 in xrealloc wrapper.c:126:8
    microsoft#2 0x93b14d in strbuf_grow strbuf.c:98:2
    microsoft#3 0x93ccf6 in strbuf_vaddf strbuf.c:392:3
    microsoft#4 0x93f726 in xstrvfmt strbuf.c:979:2
    microsoft#5 0x93f8b3 in xstrfmt strbuf.c:989:8
    microsoft#6 0x92ad8a in prefix_path_gently setup.c:115:15
    microsoft#7 0x873a8d in init_pathspec_item pathspec.c:439:11
    microsoft#8 0x87334f in parse_pathspec pathspec.c:589:3
    microsoft#9 0x646ffa in cmd_rm builtin/rm.c:266:2
    microsoft#10 0x4cd91d in run_builtin git.c:467:11
    microsoft#11 0x4cb5f3 in handle_builtin git.c:719:3
    microsoft#12 0x4ccf47 in run_argv git.c:808:4
    microsoft#13 0x4caf49 in cmd_main git.c:939:19
    microsoft#14 0x69dc0e in main common-main.c:52:11
    microsoft#15 0x7f948825b349 in __libc_start_main (/lib64/libc.so.6+0x24349)

Indirect leak of 15 byte(s) in 1 object(s) allocated from:
    #0 0x486834 in strdup ../projects/compiler-rt/lib/asan/asan_interceptors.cpp:452:3
    microsoft#1 0x9ac048 in xstrdup wrapper.c:29:14
    microsoft#2 0x873ba2 in init_pathspec_item pathspec.c:468:20
    microsoft#3 0x87334f in parse_pathspec pathspec.c:589:3
    microsoft#4 0x646ffa in cmd_rm builtin/rm.c:266:2
    microsoft#5 0x4cd91d in run_builtin git.c:467:11
    microsoft#6 0x4cb5f3 in handle_builtin git.c:719:3
    microsoft#7 0x4ccf47 in run_argv git.c:808:4
    microsoft#8 0x4caf49 in cmd_main git.c:939:19
    microsoft#9 0x69dc0e in main common-main.c:52:11
    microsoft#10 0x7f948825b349 in __libc_start_main (/lib64/libc.so.6+0x24349)

Direct leak of 1 byte(s) in 1 object(s) allocated from:
    #0 0x49a9d2 in calloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3
    microsoft#1 0x9ac392 in xcalloc wrapper.c:140:8
    microsoft#2 0x647108 in cmd_rm builtin/rm.c:294:9
    microsoft#3 0x4cd91d in run_builtin git.c:467:11
    microsoft#4 0x4cb5f3 in handle_builtin git.c:719:3
    microsoft#5 0x4ccf47 in run_argv git.c:808:4
    microsoft#6 0x4caf49 in cmd_main git.c:939:19
    microsoft#7 0x69dbfe in main common-main.c:52:11
    microsoft#8 0x7f4fac1b0349 in __libc_start_main (/lib64/libc.so.6+0x24349)

Signed-off-by: Andrzej Hunt <ajrhunt@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
ahunt authored and gitster committed Apr 28, 2021
1 parent 805b789 commit 37be119
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions builtin/rm.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
if (!seen_any)
exit(0);
}
clear_pathspec(&pathspec);
free(seen);

if (!index_only)
submodules_absorb_gitdir_if_needed();
Expand Down

0 comments on commit 37be119

Please sign in to comment.