From: Patrick Steinhardt Date: Mon, 07 Sep 2026 11:13:23 GMT Subject: Re: history: 'reword HEAD' surfaces a memory leak Message-ID: In-Reply-To: <04faa984-5b56-406b-baa9-dbb4b5eab259@app.fastmail.com> On Thu, Sep 03, 2026 at 05:26:44PM +0200, Kristoffer Haugsbakk wrote: > I get a sanitizer error when I run a simple reword command: > > $ make SANITIZE=address install > $ GIT_EDITOR=true git history reword \ > --dry-run 3cb9185f65410273787f74333cc027d2ea5daada > update HEAD eeb9d4b5c4041739c0a19b4043924513c8aad2d2 3cb9185f65410273787f74333cc027d2ea5daada > > ================================================================= > ==116999==ERROR: LeakSanitizer: detected memory leaks > > Direct leak of 283 byte(s) in 1 object(s) allocated from: > #0 0x712d2f6b4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145 > #1 0x61970a5757b2 in do_xmalloc /home/kristoffer/programming/git-worktree/git-leakcheck/wrapper.c:55 > #2 0x61970a575ec0 in do_xmallocz /home/kristoffer/programming/git-worktree/git-leakcheck/wrapper.c:89 > #3 0x61970a575ec0 in do_xmallocz /home/kristoffer/programming/git-worktree/git-leakcheck/wrapper.c:79 > #4 0x61970a575ec0 in xmallocz_gently /home/kristoffer/programming/git-worktree/git-leakcheck/wrapper.c:102 > #5 0x61970a309ca0 in unpack_compressed_entry /home/kristoffer/programming/git-worktree/git-leakcheck/packfile.c:1732 > #6 0x61970a310bf0 in unpack_entry /home/kristoffer/programming/git-worktree/git-leakcheck/packfile.c:1885 > #7 0x61970a311fbf in cache_or_unpack_entry /home/kristoffer/programming/git-worktree/git-leakcheck/packfile.c:1528 > #8 0x61970a311fbf in packed_object_info_with_index_pos /home/kristoffer/programming/git-worktree/git-leakcheck/packfile.c:1610 > #9 0x61970a313097 in packed_object_info /home/kristoffer/programming/git-worktree/git-leakcheck/packfile.c:1720 > #10 0x61970a313097 in packfile_store_read_object_info /home/kristoffer/programming/git-worktree/git-leakcheck/packfile.c:2214 > #11 0x61970a2caae8 in odb_source_files_read_object_info odb/source-files.c:58 > #12 0x61970a2c69a7 in odb_source_read_object_info odb/source.h:326 > #13 0x61970a2c69a7 in do_oid_object_info_extended /home/kristoffer/programming/git-worktree/git-leakcheck/odb.c:572 > #14 0x61970a2c69a7 in odb_read_object_info_extended /home/kristoffer/programming/git-worktree/git-leakcheck/odb.c:710 > #15 0x61970a2c7cbe in odb_read_object /home/kristoffer/programming/git-worktree/git-leakcheck/odb.c:756 > #16 0x61970a101d5a in repo_get_commit_buffer /home/kristoffer/programming/git-worktree/git-leakcheck/commit.c:399 > #17 0x61970a101d5a in repo_get_commit_buffer /home/kristoffer/programming/git-worktree/git-leakcheck/commit.c:391 > #18 0x61970a346bf6 in repo_logmsg_reencode /home/kristoffer/programming/git-worktree/git-leakcheck/pretty.c:716 > #19 0x619709ebbe64 in commit_tree_ext builtin/history.c:127 > #20 0x619709ebd17f in commit_tree_with_edited_message builtin/history.c:183 > #21 0x619709ebd17f in cmd_history_reword builtin/history.c:717 > #22 0x619709ec186f in cmd_history builtin/history.c:998 > #23 0x619709d87ca0 in run_builtin /home/kristoffer/programming/git-worktree/git-leakcheck/git.c:506 > #24 0x619709d87ca0 in handle_builtin /home/kristoffer/programming/git-worktree/git-leakcheck/git.c:782 > #25 0x619709d8c9a6 in run_argv /home/kristoffer/programming/git-worktree/git-leakcheck/git.c:865 > #26 0x619709d8c9a6 in cmd_main /home/kristoffer/programming/git-worktree/git-leakcheck/git.c:986 > #27 0x619709d83f54 in main /home/kristoffer/programming/git-worktree/git-leakcheck/common-main.c:9 > #28 0x712d2f229d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58 > > SUMMARY: AddressSanitizer: 283 byte(s) leaked in 1 allocation(s). This feels familiar, even though it doesn't reproduce on my machine. Do you have "i18n.commitencoding" set? In any case, I think the following patch should fix it: diff --git a/builtin/history.c b/builtin/history.c index 000155ad9c..bca95b9d05 100644 --- a/builtin/history.c +++ b/builtin/history.c @@ -158,6 +158,7 @@ static int commit_tree_ext(struct repository *repo, *out = lookup_commit_or_die(&rewritten_commit_oid, "rewritten commit"); out: + repo_unuse_commit_buffer(repo, commit_with_message, original_message); free_commit_extra_headers(original_extra_headers); strbuf_release(&commit_message); free(original_author); I know that we've already been discussing this memory leak on the mailing list at one point in time, and we've already had a patch similar to the above. So why wasn't this merged...? Ah, the discussion was in [1]. Cc'ing Kaartic. Thanks! Patrick [1]: https://lore.kernel.org/git/20260614141600.620272-1-kaartic.sivaraam@gmail.com/