Skip to content

Commit 371ed0d

Browse files
benpeartgitster
authored andcommitted
read-cache: clean up casting and byte decoding
This patch does a clean up pass to minimize the casting required to work with the memory mapped index (mmap). It also makes the decoding of network byte order more consistent by using get_be32() where possible. Signed-off-by: Ben Peart <benpeart@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 252d079 commit 371ed0d

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

read-cache.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ int verify_index_checksum;
16501650
/* Allow fsck to force verification of the cache entry order. */
16511651
int verify_ce_order;
16521652

1653-
static int verify_hdr(struct cache_header *hdr, unsigned long size)
1653+
static int verify_hdr(const struct cache_header *hdr, unsigned long size)
16541654
{
16551655
git_hash_ctx c;
16561656
unsigned char hash[GIT_MAX_RAWSZ];
@@ -1674,7 +1674,7 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
16741674
}
16751675

16761676
static int read_index_extension(struct index_state *istate,
1677-
const char *ext, void *data, unsigned long sz)
1677+
const char *ext, const char *data, unsigned long sz)
16781678
{
16791679
switch (CACHE_EXT(ext)) {
16801680
case CACHE_EXT_TREE:
@@ -1889,8 +1889,8 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
18891889
int fd, i;
18901890
struct stat st;
18911891
unsigned long src_offset;
1892-
struct cache_header *hdr;
1893-
void *mmap;
1892+
const struct cache_header *hdr;
1893+
const char *mmap;
18941894
size_t mmap_size;
18951895
const struct cache_entry *previous_ce = NULL;
18961896

@@ -1918,7 +1918,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
19181918
die_errno("unable to map index file");
19191919
close(fd);
19201920

1921-
hdr = mmap;
1921+
hdr = (const struct cache_header *)mmap;
19221922
if (verify_hdr(hdr, mmap_size) < 0)
19231923
goto unmap;
19241924

@@ -1943,7 +1943,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
19431943
struct cache_entry *ce;
19441944
unsigned long consumed;
19451945

1946-
disk_ce = (struct ondisk_cache_entry *)((char *)mmap + src_offset);
1946+
disk_ce = (struct ondisk_cache_entry *)(mmap + src_offset);
19471947
ce = create_from_disk(istate, disk_ce, &consumed, previous_ce);
19481948
set_index_entry(istate, i, ce);
19491949

@@ -1961,21 +1961,20 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
19611961
* in 4-byte network byte order.
19621962
*/
19631963
uint32_t extsize;
1964-
memcpy(&extsize, (char *)mmap + src_offset + 4, 4);
1965-
extsize = ntohl(extsize);
1964+
extsize = get_be32(mmap + src_offset + 4);
19661965
if (read_index_extension(istate,
1967-
(const char *) mmap + src_offset,
1968-
(char *) mmap + src_offset + 8,
1966+
mmap + src_offset,
1967+
mmap + src_offset + 8,
19691968
extsize) < 0)
19701969
goto unmap;
19711970
src_offset += 8;
19721971
src_offset += extsize;
19731972
}
1974-
munmap(mmap, mmap_size);
1973+
munmap((void *)mmap, mmap_size);
19751974
return istate->cache_nr;
19761975

19771976
unmap:
1978-
munmap(mmap, mmap_size);
1977+
munmap((void *)mmap, mmap_size);
19791978
die("index file corrupt");
19801979
}
19811980

0 commit comments

Comments
 (0)