diff options
author | Philip Kelley <phkelley@hotmail.com> | 2012-11-10 00:39:10 +0400 |
---|---|---|
committer | Philip Kelley <phkelley@hotmail.com> | 2012-11-10 00:39:10 +0400 |
commit | 2364735c8fc08615fd868244e9e00143c70c0c22 (patch) | |
tree | a8664b9a549c5274269355db9f816b6ed8d984a5 /src | |
parent | 0f674411e9358a9d42ba892e853c6abd2303f296 (diff) |
Fix implementation of strndup to not overrun
Diffstat (limited to 'src')
-rw-r--r-- | src/util.h | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/util.h b/src/util.h index 4f83d3bc1..3d00e9c85 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; |