remote: make sure the name stays valid on rename

We must make sure that the name pointer remains valid, so make sure to
allocate the new one before freeing the old one and swap them so the
user never sees an invalid pointer.
This commit is contained in:
Carlos Martín Nieto 2014-06-06 15:57:37 +02:00
parent 5a49ff9fa0
commit 61dcfe1400

View File

@ -1484,6 +1484,7 @@ int git_remote_rename(
void *payload)
{
int error;
char *tmp, *dup;
assert(remote && new_name);
@ -1510,10 +1511,12 @@ int git_remote_rename(
if ((error = rename_fetch_refspecs(remote, new_name, callback, payload)) < 0)
return error;
git__free(remote->name);
dup = git__strdup(new_name);
GITERR_CHECK_ALLOC(dup);
remote->name = git__strdup(new_name);
GITERR_CHECK_ALLOC(remote->name);
tmp = remote->name;
remote->name = dup;
git__free(tmp);
return 0;
}