Commit 2364735c8fc08615fd868244e9e00143c70c0c22

Philip Kelley 2012-11-09T15:39:10

Fix implementation of strndup to not overrun

diff --git a/src/util.h b/src/util.h
index 4f83d3b..3d00e9c 100644
--- a/src/util.h
+++ b/src/util.h
@@ -42,12 +42,11 @@ GIT_INLINE(char *) git__strdup(const char *str)
 
 GIT_INLINE(char *) git__strndup(const char *str, size_t n)
 {
-	size_t length;
+	size_t length = 0;
 	char *ptr;
 
-	length = strlen(str);
-	if (n < length)
-		length = n;
+	while (length < n && str[length])
+		++length;
 
 	ptr = (char*)malloc(length + 1);
 	if (!ptr) {
@@ -55,7 +54,9 @@ GIT_INLINE(char *) git__strndup(const char *str, size_t n)
 		return NULL;
 	}
 
-	memcpy(ptr, str, length);
+	if (length)
+		memcpy(ptr, str, length);
+
 	ptr[length] = '\0';
 
 	return ptr;