Commit 7c32a0b5f7cc3eeb0ea1f12e6d2bc0839a7f28ef

Russell Belfer 2013-01-31T13:20:20

Merge pull request #1300 from carlosmn/ref-leading-slash Crash when resolving a ref starting with "/"

diff --git a/src/refs.c b/src/refs.c
index 52e0ada..e75f510 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -1691,6 +1691,11 @@ int git_reference__normalize_name(
 			segments_count++;
 		}
 
+		/* This means that there's a leading slash in the refname */
+		if (segment_len == 0 && segments_count == 0) {
+			goto cleanup;
+		}
+
 		if (current[segment_len] == '\0')
 			break;
 
diff --git a/tests-clar/refs/isvalidname.c b/tests-clar/refs/isvalidname.c
index 3cc838d..b61a023 100644
--- a/tests-clar/refs/isvalidname.c
+++ b/tests-clar/refs/isvalidname.c
@@ -10,6 +10,8 @@ void test_refs_isvalidname__can_detect_invalid_formats(void)
 	cl_assert_equal_i(false, git_reference_is_valid_name("_NO_LEADING_UNDERSCORE"));
 	cl_assert_equal_i(false, git_reference_is_valid_name("HEAD/aa"));
 	cl_assert_equal_i(false, git_reference_is_valid_name("lower_case"));
+	cl_assert_equal_i(false, git_reference_is_valid_name("/stupid/name/master"));
+	cl_assert_equal_i(false, git_reference_is_valid_name("/"));
 	cl_assert_equal_i(false, git_reference_is_valid_name(""));
 }