Skip to content

Commit 97613b9

Browse files
pks-tgitster
authored andcommitted
transport-helper: fix leaking helper name
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fba95da commit 97613b9

File tree

6 files changed

+9
-2
lines changed

6 files changed

+9
-2
lines changed

t/t0611-reftable-httpd.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='reftable HTTPD tests'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67
. "$TEST_DIRECTORY"/lib-httpd.sh
78

t/t5563-simple-http-auth.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='test http auth header and credential helper interop'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67
. "$TEST_DIRECTORY"/lib-httpd.sh
78

t/t5564-http-proxy.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description="test fetching through http proxy"
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67
. "$TEST_DIRECTORY"/lib-httpd.sh
78

t/t5581-http-curl-verbose.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ test_description='test GIT_CURL_VERBOSE'
44
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
55
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
66

7+
TEST_PASSES_SANITIZE_LEAK=true
78
. ./test-lib.sh
89
. "$TEST_DIRECTORY"/lib-httpd.sh
910
start_httpd

transport-helper.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
static int debug;
2323

2424
struct helper_data {
25-
const char *name;
25+
char *name;
2626
struct child_process *helper;
2727
FILE *out;
2828
unsigned fetch : 1,
@@ -111,6 +111,7 @@ static void do_take_over(struct transport *transport)
111111
data = (struct helper_data *)transport->data;
112112
transport_take_over(transport, data->helper);
113113
fclose(data->out);
114+
free(data->name);
114115
free(data);
115116
}
116117

@@ -253,6 +254,7 @@ static int disconnect_helper(struct transport *transport)
253254
close(data->helper->out);
254255
fclose(data->out);
255256
res = finish_command(data->helper);
257+
FREE_AND_NULL(data->name);
256258
FREE_AND_NULL(data->helper);
257259
}
258260
return res;
@@ -1297,7 +1299,7 @@ static struct transport_vtable vtable = {
12971299
int transport_helper_init(struct transport *transport, const char *name)
12981300
{
12991301
struct helper_data *data = xcalloc(1, sizeof(*data));
1300-
data->name = name;
1302+
data->name = xstrdup(name);
13011303

13021304
transport_check_allowed(name);
13031305

transport.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,7 @@ struct transport *transport_get(struct remote *remote, const char *url)
11761176
int len = external_specification_len(url);
11771177
char *handler = xmemdupz(url, len);
11781178
transport_helper_init(ret, handler);
1179+
free(handler);
11791180
}
11801181

11811182
if (ret->smart_options) {

0 commit comments

Comments
 (0)