Commit 61dcfe1400a5ff1cf4dc805795cb72657c524906

Carlos Martín Nieto 2014-06-06T15:57:37

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.

diff --git a/src/remote.c b/src/remote.c
index dd88cf9..c7b97ca 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -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;
 }