summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2021-08-31 15:22:44 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2021-09-01 20:39:51 -0400
commit1196de4f263bdbb02c2c5b7030f159c2ff23af34 (patch)
tree32122f00dfc1082abf55d8875154e1425ed6cefa /src
parente5a3277452095a908787c3ea0279fbec21c0a76a (diff)
downloadlibgit2-1196de4f263bdbb02c2c5b7030f159c2ff23af34.tar.gz
util: introduce `git__strlcmp`
Introduce a utility function that compares a NUL terminated string to a possibly not-NUL terminated string with length. This is similar to `strncmp` but with an added check to ensure that the lengths match (not just the `size` portion of the two strings).
Diffstat (limited to 'src')
-rw-r--r--src/util.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h
index a7735366b..68c2b1804 100644
--- a/src/util.h
+++ b/src/util.h
@@ -168,6 +168,17 @@ extern int git__strncasecmp(const char *a, const char *b, size_t sz);
extern int git__strcasesort_cmp(const char *a, const char *b);
+/*
+ * Compare some NUL-terminated `a` to a possibly non-NUL terminated
+ * `b` of length `b_len`; like `strncmp` but ensuring that
+ * `strlen(a) == b_len` as well.
+ */
+GIT_INLINE(int) git__strlcmp(const char *a, const char *b, size_t b_len)
+{
+ int cmp = strncmp(a, b, b_len);
+ return cmp ? cmp : (int)a[b_len];
+}
+
typedef struct {
git_atomic32 refcount;
void *owner;