Skip to content

Commit 012b32b

Browse files
peffgitster
authored andcommitted
pack-revindex: use unsigned to store number of objects
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c334b87 commit 012b32b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

pack-revindex.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ static int cmp_offset(const void *a_, const void *b_)
7272
static void create_pack_revindex(struct pack_revindex *rix)
7373
{
7474
struct packed_git *p = rix->p;
75-
int num_ent = p->num_objects;
76-
int i;
75+
unsigned num_ent = p->num_objects;
76+
unsigned i;
7777
const char *index = p->index_data;
7878

7979
rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
@@ -114,7 +114,7 @@ static void create_pack_revindex(struct pack_revindex *rix)
114114
struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
115115
{
116116
int num;
117-
int lo, hi;
117+
unsigned lo, hi;
118118
struct pack_revindex *rix;
119119
struct revindex_entry *revindex;
120120

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

0 commit comments

Comments
 (0)