Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Kelley <phkelley@hotmail.com>2012-11-10 00:39:10 +0400
committerPhilip Kelley <phkelley@hotmail.com>2012-11-10 00:39:10 +0400
commit2364735c8fc08615fd868244e9e00143c70c0c22 (patch)
treea8664b9a549c5274269355db9f816b6ed8d984a5 /src/util.h
parent0f674411e9358a9d42ba892e853c6abd2303f296 (diff)
Fix implementation of strndup to not overrun
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h11
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;