diff options
author | brian m. carlson <sandals@crustytoothpaste.net> | 2020-05-25 22:58:50 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2020-05-27 20:07:06 +0300 |
commit | 14570dc67d2a500dfb9f33a7445bdbd6133af4ac (patch) | |
tree | e233933443b76a1833c4b904d9fc0b3c2e296245 /git-compat-util.h | |
parent | 92315e50b275deee8e84d28ee1ff1ad555a5de36 (diff) |
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 'git-compat-util.h')
-rw-r--r-- | git-compat-util.h | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/git-compat-util.h b/git-compat-util.h index 8ba576e81e..a3dcfc8613 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -869,6 +869,12 @@ FILE *fopen_for_writing(const char *path); FILE *fopen_or_warn(const char *path, const char *mode); /* + * Like strncmp, but only return zero if s is NUL-terminated and exactly len + * characters long. If it is not, consider it greater than t. + */ +int xstrncmpz(const char *s, const char *t, size_t len); + +/* * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note * that ptr is used twice, so don't pass e.g. ptr++. */ |