Skip to content

Commit b4a4f91

Browse files
committed
Fix alignment issues for strict architectures
Fixes two locations where unaligned accesses will cause bus errors on architectures that are strict about such accesses, namely sparc. The first is in swab32_into, which is called with an offset of +1 into an unsigned char array from mklink_fs. The second is in add2fs_from_tarball when checking the validity of a tarball, which casts a string from an unaligned position inside a struct to a long. After these changes, the test suite passes on sparc.
1 parent 3b99f4a commit b4a4f91

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

genext2fs.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,11 +2058,14 @@ mkdir_fs(filesystem *fs, uint32 parent_nod, const char *name, uint32 mode,
20582058

20592059
// byte swapping for symlinks
20602060
static inline void
2061-
swab32_into(uint32 *dst, uint32 *src, size_t n)
2061+
swab32_into(uint32 *dst, uint8 *src, size_t n)
20622062
{
20632063
size_t i;
2064-
for(i = 0; i < n; i++)
2065-
dst[i] = swab32(src[i]);
2064+
for(i = 0; i < n; i++) {
2065+
uint32 tmp_buf;
2066+
memcpy(&tmp_buf, &(src[i * (sizeof(uint32) / sizeof(uint8))]), sizeof(uint32) / sizeof(uint8));
2067+
dst[i] = swab32(tmp_buf);
2068+
}
20662069
}
20672070

20682071
// make a symlink
@@ -2079,7 +2082,7 @@ mklink_fs(filesystem *fs, uint32 parent_nod, const char *name, size_t size, uint
20792082

20802083
if (size < 4 * (EXT2_TIND_BLOCK + 1))
20812084
if (fs->swapit)
2082-
swab32_into(node->i_block, (uint32 *)b, EXT2_TIND_BLOCK + 1);
2085+
swab32_into(node->i_block, b, EXT2_TIND_BLOCK + 1);
20832086
else
20842087
memcpy(node->i_block, b, 4 * (EXT2_TIND_BLOCK + 1));
20852088
else
@@ -2290,7 +2293,7 @@ add2fs_from_tarball(filesystem *fs, uint32 this_nod, FILE * fh, int squash_uids,
22902293
continue;
22912294
} else
22922295
nbnull = 0;
2293-
if (*(long *)tarhead->ustar != *(long *)"ustar\00000" && strcmp(tarhead->ustar, "ustar "))
2296+
if (memcmp(tarhead->ustar, "ustar\00000", sizeof(long)) && strcmp(tarhead->ustar, "ustar "))
22942297
error_msg_and_die("not a tarball");
22952298
signed_checksum = unsigned_checksum = 0;
22962299
checksum = OCTAL_READ(tarhead->checksum);
@@ -3351,7 +3354,7 @@ print_link(filesystem *fs, uint32 nod)
33513354
uint32 *buf = malloc(4 * (EXT2_TIND_BLOCK + 1));
33523355
if (buf == NULL)
33533356
error_msg_and_die(memory_exhausted);
3354-
swab32_into(buf, node->i_block, EXT2_TIND_BLOCK + 1);
3357+
swab32_into(buf, (uint8*)node->i_block, EXT2_TIND_BLOCK + 1);
33553358
printf("links to '%s'\n", (char*) buf);
33563359
free(buf);
33573360
} else {

0 commit comments

Comments
 (0)