Merge branch 'jc/fix-alloc-sortbuf-in-index-pack'
authorJunio C Hamano <gitster@pobox.com>
Thu, 9 Jul 2015 21:31:42 +0000 (14:31 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 9 Jul 2015 21:31:42 +0000 (14:31 -0700)
A hotfix for what is in 2.5-rc but not in 2.4.

* jc/fix-alloc-sortbuf-in-index-pack:
index-pack: fix allocation of sorted_by_pos array

1  2 
builtin/index-pack.c
diff --combined builtin/index-pack.c
index 48fa4724aa01b67fd53682e8db51c8013c5bc5fe,fa13e200941ea51c345cb6b0304a0186bf35f3f4..f07bc66ed6679cac715491f0b212aca0b8bcd625
@@@ -616,9 -616,7 +616,9 @@@ static int compare_ofs_delta_bases(off_
        int cmp = type1 - type2;
        if (cmp)
                return cmp;
 -      return offset1 - offset2;
 +      return offset1 < offset2 ? -1 :
 +             offset1 > offset2 ?  1 :
 +             0;
  }
  
  static int find_ofs_delta(const off_t offset, enum object_type type)
@@@ -786,7 -784,7 +786,7 @@@ static void sha1_object(const void *dat
        assert(data || obj_entry);
  
        read_lock();
 -      collision_test_needed = has_sha1_file(sha1);
 +      collision_test_needed = has_sha1_file_with_flags(sha1, HAS_SHA1_QUICK);
        read_unlock();
  
        if (collision_test_needed && !data) {
@@@ -1053,9 -1051,7 +1053,9 @@@ static int compare_ofs_delta_entry(cons
        const struct ofs_delta_entry *delta_a = a;
        const struct ofs_delta_entry *delta_b = b;
  
 -      return delta_a->offset - delta_b->offset;
 +      return delta_a->offset < delta_b->offset ? -1 :
 +             delta_a->offset > delta_b->offset ?  1 :
 +             0;
  }
  
  static int compare_ref_delta_entry(const void *a, const void *b)
@@@ -1227,7 -1223,7 +1227,7 @@@ static void resolve_deltas(void
   * - append objects to convert thin pack to full pack if required
   * - write the final 20-byte SHA-1
   */
- static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved);
+ static void fix_unresolved_deltas(struct sha1file *f);
  static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_sha1)
  {
        if (nr_ref_deltas + nr_ofs_deltas == nr_resolved_deltas) {
                memset(objects + nr_objects + 1, 0,
                       nr_unresolved * sizeof(*objects));
                f = sha1fd(output_fd, curr_pack);
-               fix_unresolved_deltas(f, nr_unresolved);
+               fix_unresolved_deltas(f);
                strbuf_addf(&msg, _("completed with %d local objects"),
                            nr_objects - nr_objects_initial);
                stop_progress_msg(&progress, msg.buf);
@@@ -1276,6 -1272,7 +1276,6 @@@ static int write_compressed(struct sha1
        int status;
        unsigned char outbuf[4096];
  
 -      memset(&stream, 0, sizeof(stream));
        git_deflate_init(&stream, zlib_compression_level);
        stream.next_in = in;
        stream.avail_in = size;
@@@ -1331,10 -1328,10 +1331,10 @@@ static int delta_pos_compare(const voi
        return a->obj_no - b->obj_no;
  }
  
- static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved)
+ static void fix_unresolved_deltas(struct sha1file *f)
  {
        struct ref_delta_entry **sorted_by_pos;
-       int i, n = 0;
+       int i;
  
        /*
         * Since many unresolved deltas may well be themselves base objects
         * before deltas depending on them, a good heuristic is to start
         * resolving deltas in the same order as their position in the pack.
         */
-       sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
+       sorted_by_pos = xmalloc(nr_ref_deltas * sizeof(*sorted_by_pos));
        for (i = 0; i < nr_ref_deltas; i++)
-               sorted_by_pos[n++] = &ref_deltas[i];
-       qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
+               sorted_by_pos[i] = &ref_deltas[i];
+       qsort(sorted_by_pos, nr_ref_deltas, sizeof(*sorted_by_pos), delta_pos_compare);
  
-       for (i = 0; i < n; i++) {
+       for (i = 0; i < nr_ref_deltas; i++) {
                struct ref_delta_entry *d = sorted_by_pos[i];
                enum object_type type;
                struct base_data *base_obj = alloc_base_data();