]> git.ipfire.org Git - thirdparty/git.git/blobdiff - cache.h
Git 2.24
[thirdparty/git.git] / cache.h
diff --git a/cache.h b/cache.h
index 76f38f7df06ea44683c5188da71f43458810886d..04cabaac119540e89f58cd91c46b17ddb30a26ce 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -748,6 +748,19 @@ struct cache_entry *index_file_exists(struct index_state *istate, const char *na
  */
 int index_name_pos(const struct index_state *, const char *name, int namelen);
 
+/*
+ * Some functions return the negative complement of an insert position when a
+ * precise match was not found but a position was found where the entry would
+ * need to be inserted. This helper protects that logic from any integer
+ * underflow.
+ */
+static inline int index_pos_to_insert_pos(uintmax_t pos)
+{
+       if (pos > INT_MAX)
+               die("overflow: -1 - %"PRIuMAX, pos);
+       return -1 - (int)pos;
+}
+
 #define ADD_CACHE_OK_TO_ADD 1          /* Ok to add */
 #define ADD_CACHE_OK_TO_REPLACE 2      /* Ok to replace file/directory */
 #define ADD_CACHE_SKIP_DFCHECK 4       /* Ok to skip DF conflict checks */