]> git.ipfire.org Git - thirdparty/git.git/commitdiff
transport-helper: fix leaking helper name
authorPatrick Steinhardt <ps@pks.im>
Mon, 27 May 2024 11:45:56 +0000 (13:45 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 May 2024 18:19:57 +0000 (11:19 -0700)
When initializing the transport helper in `transport_get()`, we
allocate the name of the helper. We neither end up transferring
ownership of the name, nor do we free it. The associated memory thus
leaks.

Fix this memory leak by freeing the string at the calling side in
`transport_get()`. `transport_helper_init()` now creates its own copy of
the string and thus can free it as required.

An alterantive way to fix this would be to transfer ownership of the
string passed into `transport_helper_init()`, which would avoid the call
to xstrdup(1). But it does make for a more surprising calling convention
as we do not typically transfer ownership of strings like this.

Mark now-passing tests as leak free.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t0611-reftable-httpd.sh
t/t5563-simple-http-auth.sh
t/t5564-http-proxy.sh
t/t5581-http-curl-verbose.sh
transport-helper.c
transport.c

index 5e05b9c1f2ac73deac78ce6fa91e60cb377a3c68..2805995cc8c6980b6e8477c21e21987254e5e3e0 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='reftable HTTPD tests'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-httpd.sh
 
index 5d5caa3f582f0940b30b525d667eff276500b6a0..4af796de67026faf284a8663d6a140fbdf7fd7e3 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='test http auth header and credential helper interop'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-httpd.sh
 
index 9da5134614c71a29c5704199ecf44ea80aa96f52..bb35b87071d93dd5eacfeac67034f198dbf6a449 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description="test fetching through http proxy"
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-httpd.sh
 
index cded79c16b5d602dfac50305e0d5325dfa151daf..724f61005427b3b10c7f495691e50b27b633ea8d 100755 (executable)
@@ -4,6 +4,7 @@ test_description='test GIT_CURL_VERBOSE'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-httpd.sh
 start_httpd
index 780fcaf5292c315c5088736e2700b6ce937acc63..9820947ab2dee57bf4853d14a9fa3d970331aec9 100644 (file)
@@ -22,7 +22,7 @@
 static int debug;
 
 struct helper_data {
-       const char *name;
+       char *name;
        struct child_process *helper;
        FILE *out;
        unsigned fetch : 1,
@@ -111,6 +111,7 @@ static void do_take_over(struct transport *transport)
        data = (struct helper_data *)transport->data;
        transport_take_over(transport, data->helper);
        fclose(data->out);
+       free(data->name);
        free(data);
 }
 
@@ -253,6 +254,7 @@ static int disconnect_helper(struct transport *transport)
                close(data->helper->out);
                fclose(data->out);
                res = finish_command(data->helper);
+               FREE_AND_NULL(data->name);
                FREE_AND_NULL(data->helper);
        }
        return res;
@@ -1297,7 +1299,7 @@ static struct transport_vtable vtable = {
 int transport_helper_init(struct transport *transport, const char *name)
 {
        struct helper_data *data = xcalloc(1, sizeof(*data));
-       data->name = name;
+       data->name = xstrdup(name);
 
        transport_check_allowed(name);
 
index 0ad04b77fd21c561094363f60d6e8e7aa8df8047..83ddea8fbc03875b6a9683b37f1fbc27d3d2984b 100644 (file)
@@ -1176,6 +1176,7 @@ struct transport *transport_get(struct remote *remote, const char *url)
                int len = external_specification_len(url);
                char *handler = xmemdupz(url, len);
                transport_helper_init(ret, handler);
+               free(handler);
        }
 
        if (ret->smart_options) {