Skip to content

Commit

Permalink
pack-revindex: use unsigned to store number of objects
Browse files Browse the repository at this point in the history
A packfile may have up to 2^32-1 objects in it, so the
"right" data type to use is uint32_t. We currently use a
signed int, which means that we may behave incorrectly for
packfiles with more than 2^31-1 objects on 32-bit systems.

Nobody has noticed because having 2^31 objects is pretty
insane. The linux.git repo has on the order of 2^22 objects,
which is hundreds of times smaller than necessary to trigger
the bug.

Let's bump this up to an "unsigned". On 32-bit systems, this
gives us the correct data-type, and on 64-bit systems, it is
probably more efficient to use the native "unsigned" than a
true uint32_t.

While we're at it, we can fix the binary search not to
overflow in such a case if our unsigned is 32 bits.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Jul 12, 2013
1 parent c334b87 commit 012b32b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pack-revindex.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ static int cmp_offset(const void *a_, const void *b_)
static void create_pack_revindex(struct pack_revindex *rix)
{
struct packed_git *p = rix->p;
int num_ent = p->num_objects;
int i;
unsigned num_ent = p->num_objects;
unsigned i;
const char *index = p->index_data;

rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
Expand Down Expand Up @@ -114,7 +114,7 @@ static void create_pack_revindex(struct pack_revindex *rix)
struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
{
int num;
int lo, hi;
unsigned lo, hi;
struct pack_revindex *rix;
struct revindex_entry *revindex;

Expand All @@ -132,7 +132,7 @@ struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
lo = 0;
hi = p->num_objects + 1;
do {
int mi = (lo + hi) / 2;
unsigned mi = lo + (hi - lo) / 2;
if (revindex[mi].offset == ofs) {
return revindex + mi;
} else if (ofs < revindex[mi].offset)
Expand Down

0 comments on commit 012b32b

Please sign in to comment.