Commit 8fe713ccf7bf8c6330fdda7f0c733e7f3ab29d3f

Russell Belfer 2012-12-19T15:06:14

Make git_oid_tostr use out buffer for NULL oid Previously a NULL oid was handled like an empty buffer and returned a status empty string. This makes git_oid_tostr() set the output buffer to the empty string instead.

diff --git a/src/oid.c b/src/oid.c
index 1bf74b9..bbdd854 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -95,12 +95,12 @@ char *git_oid_tostr(char *out, size_t n, const git_oid *oid)
 {
 	char str[GIT_OID_HEXSZ];
 
-	if (!out || n == 0 || !oid)
+	if (!out || n == 0)
 		return "";
 
 	n--; /* allow room for terminating NUL */
 
-	if (n > 0) {
+	if (n > 0 && oid != NULL) {
 		git_oid_fmt(str, oid);
 		if (n > GIT_OID_HEXSZ)
 			n = GIT_OID_HEXSZ;
diff --git a/tests-clar/object/raw/convert.c b/tests-clar/object/raw/convert.c
index 7f310dd..74442c1 100644
--- a/tests-clar/object/raw/convert.c
+++ b/tests-clar/object/raw/convert.c
@@ -21,9 +21,9 @@ void test_object_raw_convert__succeed_on_oid_to_string_conversion(void)
 	str = git_oid_tostr(out, 0, &in);
 	cl_assert(str && *str == '\0' && str != out);
 
-	/* NULL oid pointer, returns static empty string */
+	/* NULL oid pointer, sets existing buffer to empty string */
 	str = git_oid_tostr(out, sizeof(out), NULL);
-	cl_assert(str && *str == '\0' && str != out);
+	cl_assert(str && *str == '\0' && str == out);
 
 	/* n == 1, returns out as an empty string */
 	str = git_oid_tostr(out, 1, &in);