]> git.proxmox.com Git - libgit2.git/blobdiff - src/hashsig.c
New upstream version 1.3.0+dfsg.1
[libgit2.git] / src / hashsig.c
index 14ec34b2f01683744f6c012478da06f2d3d04a28..6b4fb835216bd9d5b6d73532f5383f4e45986c82 100644 (file)
@@ -17,7 +17,7 @@ typedef uint64_t hashsig_state;
 #define HASHSIG_SCALE 100
 
 #define HASHSIG_MAX_RUN 80
-#define HASHSIG_HASH_START     0x012345678ABCDEF0LL
+#define HASHSIG_HASH_START     INT64_C(0x012345678ABCDEF0)
 #define HASHSIG_HASH_SHIFT  5
 
 #define HASHSIG_HASH_MIX(S,CH) \
@@ -133,13 +133,13 @@ typedef struct {
        uint8_t ignore_ch[256];
 } hashsig_in_progress;
 
-static void hashsig_in_progress_init(
+static int hashsig_in_progress_init(
        hashsig_in_progress *prog, git_hashsig *sig)
 {
        int i;
 
        /* no more than one can be set */
-       assert(!(sig->opt & GIT_HASHSIG_IGNORE_WHITESPACE) ||
+       GIT_ASSERT(!(sig->opt & GIT_HASHSIG_IGNORE_WHITESPACE) ||
                   !(sig->opt & GIT_HASHSIG_SMART_WHITESPACE));
 
        if (sig->opt & GIT_HASHSIG_IGNORE_WHITESPACE) {
@@ -153,6 +153,8 @@ static void hashsig_in_progress_init(
        } else {
                memset(prog, 0, sizeof(*prog));
        }
+
+       return 0;
 }
 
 static int hashsig_add_hashes(
@@ -251,7 +253,8 @@ int git_hashsig_create(
        git_hashsig *sig = hashsig_alloc(opts);
        GIT_ERROR_CHECK_ALLOC(sig);
 
-       hashsig_in_progress_init(&prog, sig);
+       if ((error = hashsig_in_progress_init(&prog, sig)) < 0)
+               return error;
 
        error = hashsig_add_hashes(sig, (const uint8_t *)buf, buflen, &prog);
 
@@ -283,7 +286,10 @@ int git_hashsig_create_fromfile(
                return fd;
        }
 
-       hashsig_in_progress_init(&prog, sig);
+       if ((error = hashsig_in_progress_init(&prog, sig)) < 0) {
+               p_close(fd);
+               return error;
+       }
 
        while (!error) {
                if ((buflen = p_read(fd, buf, sizeof(buf))) <= 0) {
@@ -318,7 +324,7 @@ static int hashsig_heap_compare(const hashsig_heap *a, const hashsig_heap *b)
 {
        int matches = 0, i, j, cmp;
 
-       assert(a->cmp == b->cmp);
+       GIT_ASSERT_WITH_RETVAL(a->cmp == b->cmp, 0);
 
        /* hash heaps are sorted - just look for overlap vs total */
 
@@ -354,9 +360,16 @@ int git_hashsig_compare(const git_hashsig *a, const git_hashsig *b)
        /* if we have fewer than the maximum number of elements, then just use
         * one array since the two arrays will be the same
         */
-       if (a->mins.size < HASHSIG_HEAP_SIZE)
+       if (a->mins.size < HASHSIG_HEAP_SIZE) {
                return hashsig_heap_compare(&a->mins, &b->mins);
-       else
-               return (hashsig_heap_compare(&a->mins, &b->mins) +
-                               hashsig_heap_compare(&a->maxs, &b->maxs)) / 2;
+       } else {
+               int mins, maxs;
+
+               if ((mins = hashsig_heap_compare(&a->mins, &b->mins)) < 0)
+                       return mins;
+               if ((maxs = hashsig_heap_compare(&a->maxs, &b->maxs)) < 0)
+                       return maxs;
+
+               return (mins + maxs) / 2;
+       }
 }