@matheusbsbs / matheus-bagliano / commits / 326bf39677
Fork 0

Use uint32_t for all packed object counts.

As we permit up to 2^32-1 objects in a single packfile we cannot use a signed int to represent the object offset within a packfile, after 2^31-1 objects we will start seeing negative indexes and error out or compute bad addresses within the mmap'd index. This is a minor cleanup that does not introduce any significant logic changes. It is roach free. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Mar 6, 2007 at 20:44 UTC 326bf396778bc9ab8f9e253bb514e7f41ef1502b
5 files changed +15 -15
builtin-fsck.c
+1 -1
@@ -632,7 +632,7 @@ int cmd_fsck(int argc, char **argv, const char *prefix)
632 verify_pack(p, 0);
633
634 for (p = packed_git; p; p = p->next) {
635 - int num = num_packed_objects(p);
635 + uint32_t i, num = num_packed_objects(p);
636 for (i = 0; i < num; i++) {
637 unsigned char sha1[20];
638 nth_packed_object_sha1(p, i, sha1);
cache.h
+2 -2
@@ -425,8 +425,8 @@ extern void pack_report(void);
425 extern unsigned char* use_pack(struct packed_git *, struct pack_window **, unsigned long, unsigned int *);
426 extern void unuse_pack(struct pack_window **);
427 extern struct packed_git *add_packed_git(char *, int, int);
428 -extern int num_packed_objects(const struct packed_git *p);
429 -extern int nth_packed_object_sha1(const struct packed_git *, int, unsigned char*);
428 +extern uint32_t num_packed_objects(const struct packed_git *p);
429 +extern int nth_packed_object_sha1(const struct packed_git *, uint32_t, unsigned char*);
430 extern unsigned long find_pack_entry_one(const unsigned char *, struct packed_git *);
431 extern void *unpack_entry(struct packed_git *, unsigned long, enum object_type *, unsigned long *);
432 extern unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
pack-check.c
+4 -4
@@ -9,7 +9,8 @@ static int verify_packfile(struct packed_git *p,
9 SHA_CTX ctx;
10 unsigned char sha1[20];
11 unsigned long offset = 0, pack_sig = p->pack_size - 20;
12 - int nr_objects, err, i;
12 + uint32_t nr_objects, i;
13 + int err;
14
15 /* Note that the pack header checks are actually performed by
16 * use_pack when it first opens the pack file. If anything
@@ -40,7 +41,7 @@ static int verify_packfile(struct packed_git *p,
41 * we do not do scan-streaming check on the pack file.
42 */
43 nr_objects = num_packed_objects(p);
43 - for (i = err = 0; i < nr_objects; i++) {
44 + for (i = 0, err = 0; i < nr_objects; i++) {
45 unsigned char sha1[20];
46 void *data;
47 enum object_type type;
@@ -74,8 +75,7 @@ static int verify_packfile(struct packed_git *p,
75
76 static void show_pack_info(struct packed_git *p)
77 {
77 - int nr_objects, i;
78 - unsigned int chain_histogram[MAX_CHAIN];
78 + uint32_t nr_objects, i, chain_histogram[MAX_CHAIN];
79
80 nr_objects = num_packed_objects(p);
81 memset(chain_histogram, 0, sizeof(chain_histogram));
sha1_file.c
+5 -5
@@ -437,7 +437,7 @@ static int check_packed_git_idx(const char *path,
437 void *idx_map;
438 uint32_t *index;
439 unsigned long idx_size;
440 - int nr, i;
440 + uint32_t nr, i;
441 int fd = open(path, O_RDONLY);
442 struct stat st;
443 if (fd < 0)
@@ -472,7 +472,7 @@ static int check_packed_git_idx(const char *path,
472
473 nr = 0;
474 for (i = 0; i < 256; i++) {
475 - unsigned int n = ntohl(index[i]);
475 + uint32_t n = ntohl(index[i]);
476 if (n < nr) {
477 munmap(idx_map, idx_size);
478 return error("non-monotonic index %s", path);
@@ -1339,17 +1339,17 @@ void *unpack_entry(struct packed_git *p, unsigned long obj_offset,
1339 return data;
1340 }
1341
1342 -int num_packed_objects(const struct packed_git *p)
1342 +uint32_t num_packed_objects(const struct packed_git *p)
1343 {
1344 /* See check_packed_git_idx() */
1345 return (p->index_size - 20 - 20 - 4*256) / 24;
1346 }
1347
1348 -int nth_packed_object_sha1(const struct packed_git *p, int n,
1348 +int nth_packed_object_sha1(const struct packed_git *p, uint32_t n,
1349 unsigned char* sha1)
1350 {
1351 void *index = p->index_base + 256;
1352 - if (n < 0 || num_packed_objects(p) <= n)
1352 + if (num_packed_objects(p) <= n)
1353 return -1;
1354 hashcpy(sha1, (unsigned char *) index + (24 * n) + 4);
1355 return 0;
sha1_name.c
+3 -3
@@ -76,10 +76,10 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
76
77 prepare_packed_git();
78 for (p = packed_git; p && found < 2; p = p->next) {
79 - unsigned num = num_packed_objects(p);
80 - unsigned first = 0, last = num;
79 + uint32_t num = num_packed_objects(p);
80 + uint32_t first = 0, last = num;
81 while (first < last) {
82 - unsigned mid = (first + last) / 2;
82 + uint32_t mid = (first + last) / 2;
83 unsigned char now[20];
84 int cmp;
85