Skip to content

Commit 9e0f45f

Browse files
jonathantanmygitster
authored andcommitted
pack: move check_pack_index_ptr(), nth_packed_object_offset()
Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d5a1676 commit 9e0f45f

File tree

4 files changed

+49
-49
lines changed

4 files changed

+49
-49
lines changed

cache.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,22 +1620,6 @@ extern int odb_mkstemp(struct strbuf *template, const char *pattern);
16201620
*/
16211621
extern int odb_pack_keep(const char *name);
16221622

1623-
/*
1624-
* Make sure that a pointer access into an mmap'd index file is within bounds,
1625-
* and can provide at least 8 bytes of data.
1626-
*
1627-
* Note that this is only necessary for variable-length segments of the file
1628-
* (like the 64-bit extended offset table), as we compare the size to the
1629-
* fixed-length parts when we open the file.
1630-
*/
1631-
extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr);
1632-
1633-
/*
1634-
* Return the offset of the nth object within the specified packfile.
1635-
* The index must already be opened.
1636-
*/
1637-
extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
1638-
16391623
/*
16401624
* If the object named sha1 is present in the specified packfile,
16411625
* return its offset within the packfile; otherwise, return 0.

packfile.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,3 +1667,36 @@ const struct object_id *nth_packed_object_oid(struct object_id *oid,
16671667
hashcpy(oid->hash, hash);
16681668
return oid;
16691669
}
1670+
1671+
void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
1672+
{
1673+
const unsigned char *ptr = vptr;
1674+
const unsigned char *start = p->index_data;
1675+
const unsigned char *end = start + p->index_size;
1676+
if (ptr < start)
1677+
die(_("offset before start of pack index for %s (corrupt index?)"),
1678+
p->pack_name);
1679+
/* No need to check for underflow; .idx files must be at least 8 bytes */
1680+
if (ptr >= end - 8)
1681+
die(_("offset beyond end of pack index for %s (truncated index?)"),
1682+
p->pack_name);
1683+
}
1684+
1685+
off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
1686+
{
1687+
const unsigned char *index = p->index_data;
1688+
index += 4 * 256;
1689+
if (p->index_version == 1) {
1690+
return ntohl(*((uint32_t *)(index + 24 * n)));
1691+
} else {
1692+
uint32_t off;
1693+
index += 8 + p->num_objects * (20 + 4);
1694+
off = ntohl(*((uint32_t *)(index + 4 * n)));
1695+
if (!(off & 0x80000000))
1696+
return off;
1697+
index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
1698+
check_pack_index_ptr(p, index);
1699+
return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
1700+
ntohl(*((uint32_t *)(index + 4)));
1701+
}
1702+
}

packfile.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ extern void unuse_pack(struct pack_window **);
6363
extern void clear_delta_base_cache(void);
6464
extern struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
6565

66+
/*
67+
* Make sure that a pointer access into an mmap'd index file is within bounds,
68+
* and can provide at least 8 bytes of data.
69+
*
70+
* Note that this is only necessary for variable-length segments of the file
71+
* (like the 64-bit extended offset table), as we compare the size to the
72+
* fixed-length parts when we open the file.
73+
*/
74+
extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr);
75+
6676
/*
6777
* Return the SHA-1 of the nth object within the specified packfile.
6878
* Open the index if it is not already open. The return value points
@@ -77,6 +87,11 @@ extern const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t
7787
*/
7888
extern const struct object_id *nth_packed_object_oid(struct object_id *, struct packed_git *, uint32_t n);
7989

90+
/*
91+
* Return the offset of the nth object within the specified packfile.
92+
* The index must already be opened.
93+
*/
94+
extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
8095

8196
extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
8297
extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
@@ -94,4 +109,5 @@ extern int packed_object_info(struct packed_git *pack, off_t offset, struct obje
94109

95110
extern void mark_bad_packed_object(struct packed_git *p, const unsigned char *sha1);
96111
extern const struct packed_git *has_packed_and_bad(const unsigned char *sha1);
112+
97113
#endif

sha1_file.c

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,39 +1075,6 @@ int parse_sha1_header(const char *hdr, unsigned long *sizep)
10751075
return parse_sha1_header_extended(hdr, &oi, 0);
10761076
}
10771077

1078-
void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
1079-
{
1080-
const unsigned char *ptr = vptr;
1081-
const unsigned char *start = p->index_data;
1082-
const unsigned char *end = start + p->index_size;
1083-
if (ptr < start)
1084-
die(_("offset before start of pack index for %s (corrupt index?)"),
1085-
p->pack_name);
1086-
/* No need to check for underflow; .idx files must be at least 8 bytes */
1087-
if (ptr >= end - 8)
1088-
die(_("offset beyond end of pack index for %s (truncated index?)"),
1089-
p->pack_name);
1090-
}
1091-
1092-
off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
1093-
{
1094-
const unsigned char *index = p->index_data;
1095-
index += 4 * 256;
1096-
if (p->index_version == 1) {
1097-
return ntohl(*((uint32_t *)(index + 24 * n)));
1098-
} else {
1099-
uint32_t off;
1100-
index += 8 + p->num_objects * (20 + 4);
1101-
off = ntohl(*((uint32_t *)(index + 4 * n)));
1102-
if (!(off & 0x80000000))
1103-
return off;
1104-
index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
1105-
check_pack_index_ptr(p, index);
1106-
return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
1107-
ntohl(*((uint32_t *)(index + 4)));
1108-
}
1109-
}
1110-
11111078
off_t find_pack_entry_one(const unsigned char *sha1,
11121079
struct packed_git *p)
11131080
{

0 commit comments

Comments
 (0)