Skip to content

Commit 5285cdc

Browse files
peffdscho
authored andcommitted
parse_pack_header_option(): avoid unaligned memory writes
In order to recreate a pack header in our in-memory buffer, we cast the buffer to a "struct pack_header" and assign the individual fields. This is reported to cause SIGBUS on sparc64 due to alignment issues. We can work around this by using put_be32() which will write individual bytes into the buffer. Reported-by: Koakuma <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 2dbdf2c commit 5285cdc

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

packfile.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,17 +2318,20 @@ int is_promisor_object(struct repository *r, const struct object_id *oid)
23182318

23192319
int parse_pack_header_option(const char *in, unsigned char *out, unsigned int *len)
23202320
{
2321-
struct pack_header *hdr;
2321+
unsigned char *hdr;
23222322
char *c;
23232323

2324-
hdr = (struct pack_header *)out;
2325-
hdr->hdr_signature = htonl(PACK_SIGNATURE);
2326-
hdr->hdr_version = htonl(strtoul(in, &c, 10));
2324+
hdr = out;
2325+
put_be32(hdr, PACK_SIGNATURE);
2326+
hdr += 4;
2327+
put_be32(hdr, strtoul(in, &c, 10));
2328+
hdr += 4;
23272329
if (*c != ',')
23282330
return -1;
2329-
hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
2331+
put_be32(hdr, strtoul(c + 1, &c, 10));
2332+
hdr += 4;
23302333
if (*c)
23312334
return -1;
2332-
*len = sizeof(*hdr);
2335+
*len = hdr - out;
23332336
return 0;
23342337
}

0 commit comments

Comments
 (0)