Skip to content

Commit 422cd59

Browse files
authored
Merge pull request libgit2#4655 from glaubitz/alignment
index: Fix alignment issues in write_disk_entry()
2 parents 534b70a + 93271f5 commit 422cd59

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/index.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,7 +2629,7 @@ static bool is_index_extended(git_index *index)
26292629
static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const char *last)
26302630
{
26312631
void *mem = NULL;
2632-
struct entry_short *ondisk;
2632+
struct entry_short ondisk;
26332633
size_t path_len, disk_size;
26342634
int varint_len = 0;
26352635
char *path;
@@ -2657,9 +2657,7 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha
26572657
if (git_filebuf_reserve(file, &mem, disk_size) < 0)
26582658
return -1;
26592659

2660-
ondisk = (struct entry_short *)mem;
2661-
2662-
memset(ondisk, 0x0, disk_size);
2660+
memset(mem, 0x0, disk_size);
26632661

26642662
/**
26652663
* Yes, we have to truncate.
@@ -2671,30 +2669,32 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha
26712669
*
26722670
* In 2038 I will be either too dead or too rich to care about this
26732671
*/
2674-
ondisk->ctime.seconds = htonl((uint32_t)entry->ctime.seconds);
2675-
ondisk->mtime.seconds = htonl((uint32_t)entry->mtime.seconds);
2676-
ondisk->ctime.nanoseconds = htonl(entry->ctime.nanoseconds);
2677-
ondisk->mtime.nanoseconds = htonl(entry->mtime.nanoseconds);
2678-
ondisk->dev = htonl(entry->dev);
2679-
ondisk->ino = htonl(entry->ino);
2680-
ondisk->mode = htonl(entry->mode);
2681-
ondisk->uid = htonl(entry->uid);
2682-
ondisk->gid = htonl(entry->gid);
2683-
ondisk->file_size = htonl((uint32_t)entry->file_size);
2672+
ondisk.ctime.seconds = htonl((uint32_t)entry->ctime.seconds);
2673+
ondisk.mtime.seconds = htonl((uint32_t)entry->mtime.seconds);
2674+
ondisk.ctime.nanoseconds = htonl(entry->ctime.nanoseconds);
2675+
ondisk.mtime.nanoseconds = htonl(entry->mtime.nanoseconds);
2676+
ondisk.dev = htonl(entry->dev);
2677+
ondisk.ino = htonl(entry->ino);
2678+
ondisk.mode = htonl(entry->mode);
2679+
ondisk.uid = htonl(entry->uid);
2680+
ondisk.gid = htonl(entry->gid);
2681+
ondisk.file_size = htonl((uint32_t)entry->file_size);
26842682

2685-
git_oid_cpy(&ondisk->oid, &entry->id);
2683+
git_oid_cpy(&ondisk.oid, &entry->id);
26862684

2687-
ondisk->flags = htons(entry->flags);
2685+
ondisk.flags = htons(entry->flags);
26882686

26892687
if (entry->flags & GIT_IDXENTRY_EXTENDED) {
2690-
struct entry_long *ondisk_ext;
2691-
ondisk_ext = (struct entry_long *)ondisk;
2692-
ondisk_ext->flags_extended = htons(entry->flags_extended &
2688+
struct entry_long ondisk_ext;
2689+
memcpy(&ondisk_ext, &ondisk, sizeof(struct entry_short));
2690+
ondisk_ext.flags_extended = htons(entry->flags_extended &
26932691
GIT_IDXENTRY_EXTENDED_FLAGS);
2694-
path = ondisk_ext->path;
2692+
memcpy(mem, &ondisk_ext, offsetof(struct entry_long, path));
2693+
path = ((struct entry_long*)mem)->path;
26952694
disk_size -= offsetof(struct entry_long, path);
26962695
} else {
2697-
path = ondisk->path;
2696+
memcpy(mem, &ondisk, offsetof(struct entry_short, path));
2697+
path = ((struct entry_short*)mem)->path;
26982698
disk_size -= offsetof(struct entry_short, path);
26992699
}
27002700

0 commit comments

Comments
 (0)