Skip to content

Commit aa0595f

Browse files
peffgitster
authored andcommitted
remote: refactor alias_url() memory ownership
The alias_url() function may return either a newly allocated string (which the caller must take ownership of), or the original const "url" parameter that was passed in. This often works OK because callers are generally passing in a "url" that they expect to retain ownership of anyway. So whether we got back the original or a new string, we're always interested in storing it forever. But I suspect there are some possible leaks here (e.g., add_url_alias() may end up discarding the original "url"). Whether there are active leaks or not, this is a confusing setup that makes further refactoring of memory ownership harder. So instead of returning the original string, return NULL, forcing callers to decide what to do with it explicitly. We can then build further cleanups on top of that. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0295ce7 commit aa0595f

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

remote.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static int valid_remote(const struct remote *remote)
3535
return (!!remote->url) || (!!remote->foreign_vcs);
3636
}
3737

38-
static const char *alias_url(const char *url, struct rewrites *r)
38+
static char *alias_url(const char *url, struct rewrites *r)
3939
{
4040
int i, j;
4141
struct counted_string *longest;
@@ -56,7 +56,7 @@ static const char *alias_url(const char *url, struct rewrites *r)
5656
}
5757
}
5858
if (!longest)
59-
return url;
59+
return NULL;
6060

6161
return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
6262
}
@@ -76,15 +76,16 @@ static void add_pushurl(struct remote *remote, const char *pushurl)
7676
static void add_pushurl_alias(struct remote_state *remote_state,
7777
struct remote *remote, const char *url)
7878
{
79-
const char *pushurl = alias_url(url, &remote_state->rewrites_push);
80-
if (pushurl != url)
81-
add_pushurl(remote, pushurl);
79+
char *alias = alias_url(url, &remote_state->rewrites_push);
80+
if (alias)
81+
add_pushurl(remote, alias);
8282
}
8383

8484
static void add_url_alias(struct remote_state *remote_state,
8585
struct remote *remote, const char *url)
8686
{
87-
add_url(remote, alias_url(url, &remote_state->rewrites));
87+
char *alias = alias_url(url, &remote_state->rewrites);
88+
add_url(remote, alias ? alias : url);
8889
add_pushurl_alias(remote_state, remote, url);
8990
}
9091

@@ -492,19 +493,22 @@ static void alias_all_urls(struct remote_state *remote_state)
492493
if (!remote_state->remotes[i])
493494
continue;
494495
for (j = 0; j < remote_state->remotes[i]->pushurl_nr; j++) {
495-
remote_state->remotes[i]->pushurl[j] =
496-
alias_url(remote_state->remotes[i]->pushurl[j],
497-
&remote_state->rewrites);
496+
char *alias = alias_url(remote_state->remotes[i]->pushurl[j],
497+
&remote_state->rewrites);
498+
if (alias)
499+
remote_state->remotes[i]->pushurl[j] = alias;
498500
}
499501
add_pushurl_aliases = remote_state->remotes[i]->pushurl_nr == 0;
500502
for (j = 0; j < remote_state->remotes[i]->url_nr; j++) {
503+
char *alias;
501504
if (add_pushurl_aliases)
502505
add_pushurl_alias(
503506
remote_state, remote_state->remotes[i],
504507
remote_state->remotes[i]->url[j]);
505-
remote_state->remotes[i]->url[j] =
506-
alias_url(remote_state->remotes[i]->url[j],
508+
alias = alias_url(remote_state->remotes[i]->url[j],
507509
&remote_state->rewrites);
510+
if (alias)
511+
remote_state->remotes[i]->url[j] = alias;
508512
}
509513
}
510514
}

0 commit comments

Comments
 (0)