Introduce git__substrdup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
diff --git a/src/netops.c b/src/netops.c
index fd788bc..cc94d03 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -606,26 +606,26 @@ int gitno_extract_url_parts(
start = url;
if (at && at < slash) {
start = at+1;
- *username = git__strndup(url, at - url);
+ *username = git__substrdup(url, at - url);
}
if (colon && colon < at) {
git__free(*username);
- *username = git__strndup(url, colon-url);
- *password = git__strndup(colon+1, at-colon-1);
+ *username = git__substrdup(url, colon-url);
+ *password = git__substrdup(colon+1, at-colon-1);
colon = strchr(at, ':');
}
if (colon == NULL) {
*port = git__strdup(default_port);
} else {
- *port = git__strndup(colon + 1, slash - colon - 1);
+ *port = git__substrdup(colon + 1, slash - colon - 1);
}
GITERR_CHECK_ALLOC(*port);
end = colon == NULL ? slash : colon;
- *host = git__strndup(start, end - start);
+ *host = git__substrdup(start, end - start);
GITERR_CHECK_ALLOC(*host);
return 0;
diff --git a/src/util.h b/src/util.h
index e75d777..e9edd84 100644
--- a/src/util.h
+++ b/src/util.h
@@ -51,11 +51,7 @@ GIT_INLINE(char *) git__strndup(const char *str, size_t n)
while (length < n && str[length])
++length;
- ptr = (char*)malloc(length + 1);
- if (!ptr) {
- giterr_set_oom();
- return NULL;
- }
+ ptr = (char*)git__malloc(length + 1);
if (length)
memcpy(ptr, str, length);
@@ -65,6 +61,14 @@ GIT_INLINE(char *) git__strndup(const char *str, size_t n)
return ptr;
}
+/* NOTE: This doesn't do null or '\0' checking. Watch those boundaries! */
+GIT_INLINE(char *) git__substrdup(const char *start, size_t n)
+{
+ char *ptr = (char*)git__calloc(n+1, sizeof(char));
+ memcpy(ptr, start, n);
+ return ptr;
+}
+
GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
{
void *new_ptr = realloc(ptr, size);