summaryrefslogtreecommitdiff
path: root/wrapper.c
diff options
context:
space:
mode:
authorbrian m. carlson <sandals@crustytoothpaste.net>2020-05-25 19:58:50 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-05-27 17:07:06 (GMT)
commit14570dc67d2a500dfb9f33a7445bdbd6133af4ac (patch)
treee233933443b76a1833c4b904d9fc0b3c2e296245 /wrapper.c
parent92315e50b275deee8e84d28ee1ff1ad555a5de36 (diff)
downloadgit-14570dc67d2a500dfb9f33a7445bdbd6133af4ac.zip
git-14570dc67d2a500dfb9f33a7445bdbd6133af4ac.tar.gz
git-14570dc67d2a500dfb9f33a7445bdbd6133af4ac.tar.bz2
wrapper: add function to compare strings with different NUL termination
When parsing capabilities for the pack protocol, there are times we'll want to compare the value of a capability to a NUL-terminated string. Since the data we're reading will be space-terminated, not NUL-terminated, we need a function that compares the two strings, but also checks that they're the same length. Otherwise, if we used strncmp to compare these strings, we might accidentally accept a parameter that was a prefix of the expected value. Add a function, xstrncmpz, that takes a NUL-terminated string and a non-NUL-terminated string, plus a length, and compares them, ensuring that they are the same length. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'wrapper.c')
-rw-r--r--wrapper.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/wrapper.c b/wrapper.c
index 3a1c0e0..4ff4a9c 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -105,6 +105,14 @@ char *xstrndup(const char *str, size_t len)
return xmemdupz(str, p ? p - str : len);
}
+int xstrncmpz(const char *s, const char *t, size_t len)
+{
+ int res = strncmp(s, t, len);
+ if (res)
+ return res;
+ return s[len] == '\0' ? 0 : 1;
+}
+
void *xrealloc(void *ptr, size_t size)
{
void *ret;