builtin/grep: fix leaking object context

Even when `get_oid_with_context()` fails it may have allocated some data in the object context. But we do not release it in git-grep(1) when the call fails, leading to a memory leak. Plug it. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Sep 5, 2024 at 12:09 UTC ee087c29c89ad54980f2521641f5f182f6adbc79
3 files changed +4
builtin/grep.c
+1
@@ -1133,6 +1133,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1133 &oid, &oc)) {
1134 if (seen_dashdash)
1135 die(_("unable to resolve revision: %s"), arg);
1136 + object_context_release(&oc);
1137 break;
1138 }
1139
t/t6132-pathspec-exclude.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='test case exclude pathspec'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success 'setup' '
t/t6135-pathspec-with-attrs.sh
+2
@@ -1,6 +1,8 @@
1 #!/bin/sh
2
3 test_description='test labels in pathspecs'
4 +
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success 'setup a tree' '