Commit 31e5909214cbfba0c43143288a77f2fc067004f7

schu 2011-08-17T15:20:43

git__strndup: immediately return NULL when ENOMEM Signed-off-by: schu <schu-github@schulog.org>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
diff --git a/src/util.h b/src/util.h
index 18929af..f70bfe7 100644
--- a/src/util.h
+++ b/src/util.h
@@ -47,11 +47,13 @@ GIT_INLINE(char *) git__strndup(const char *str, size_t n)
 		length = n;
 
 	ptr = (char*)malloc(length + 1);
-	if (!ptr)
+	if (!ptr) {
 		git__throw(GIT_ENOMEM, "Out of memory. Failed to duplicate string");
+		return NULL;
+	}
 
 	memcpy(ptr, str, length);
-	ptr[length] = 0;
+	ptr[length] = '\0';
 
 	return ptr;
 }