Skip to content

Commit dfe6e66

Browse files
committed
internal: Rewrite byteswapping
The real motivation here is to rewrite the code in order to complete the relicensing, because the person who last touched this didn't respond. And I don't think we need these static assertions; let's just audit the callers to be sure they're passing the right types. Signed-off-by: Colin Walters <[email protected]>
1 parent 39e9f46 commit dfe6e66

File tree

2 files changed

+15
-48
lines changed

2 files changed

+15
-48
lines changed

libcomposefs/lcfs-internal.h

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -75,47 +75,13 @@ typedef int errint_t;
7575

7676
#define LCFS_MAX_NAME_LENGTH 255 /* max len of file name excluding NULL */
7777

78-
#define lcfs_u16_to_file(v) \
79-
({ \
80-
_Static_assert(sizeof(v) == sizeof(uint16_t), \
81-
"Size of v is not equal to size of uint16_t"); \
82-
htole16(v); \
83-
})
84-
85-
#define lcfs_u32_to_file(v) \
86-
({ \
87-
_Static_assert(sizeof(v) == sizeof(uint32_t), \
88-
"Size of v is not equal to size of uint32_t"); \
89-
htole32(v); \
90-
})
91-
92-
#define lcfs_u64_to_file(v) \
93-
({ \
94-
_Static_assert(sizeof(v) == sizeof(uint64_t), \
95-
"Size of v is not equal to size of uint64_t"); \
96-
htole64(v); \
97-
})
98-
99-
#define lcfs_u16_from_file(v) \
100-
({ \
101-
_Static_assert(sizeof(v) == sizeof(uint16_t), \
102-
"Size of v is not equal to size of uint16_t"); \
103-
le16toh(v); \
104-
})
105-
106-
#define lcfs_u32_from_file(v) \
107-
({ \
108-
_Static_assert(sizeof(v) == sizeof(uint32_t), \
109-
"Size of v is not equal to size of uint32_t"); \
110-
le32toh(v); \
111-
})
112-
113-
#define lcfs_u64_from_file(v) \
114-
({ \
115-
_Static_assert(sizeof(v) == sizeof(uint64_t), \
116-
"Size of v is not equal to size of uint64_t"); \
117-
le64toh(v); \
118-
})
78+
/* Alias macros for the generic conversions; we can drop these at some point */
79+
#define lcfs_u16_to_file(v) (htole16(v))
80+
#define lcfs_u32_to_file(v) (htole32(v))
81+
#define lcfs_u64_to_file(v) (htole64(v))
82+
#define lcfs_u16_from_file(v) (le16toh(v))
83+
#define lcfs_u32_from_file(v) (le32toh(v))
84+
#define lcfs_u64_from_file(v) (le64toh(v))
11985

12086
/* In memory representation used to build the file. */
12187

libcomposefs/lcfs-writer-erofs.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ static int write_erofs_inode_data(struct lcfs_ctx_s *ctx, struct lcfs_node_s *no
948948
i.i_ino = lcfs_u32_to_file(node->inode_num);
949949
i.i_uid = lcfs_u32_to_file(node->inode.st_uid);
950950
i.i_gid = lcfs_u32_to_file(node->inode.st_gid);
951-
i.i_mtime = lcfs_u64_to_file(node->inode.st_mtim_sec);
951+
i.i_mtime = lcfs_u64_to_file((uint64_t)node->inode.st_mtim_sec);
952952
i.i_mtime_nsec = lcfs_u32_to_file(node->inode.st_mtim_nsec);
953953

954954
if (type == S_IFDIR) {
@@ -1398,13 +1398,13 @@ int lcfs_write_erofs_to(struct lcfs_ctx_s *ctx)
13981398
struct lcfs_ctx_erofs_s *ctx_erofs = (struct lcfs_ctx_erofs_s *)ctx;
13991399
struct lcfs_node_s *root;
14001400
struct lcfs_erofs_header_s header = {
1401-
.magic = lcfs_u32_to_file(LCFS_EROFS_MAGIC),
1402-
.version = lcfs_u32_to_file(LCFS_EROFS_VERSION),
1401+
.magic = lcfs_u32_to_file(((uint32_t)LCFS_EROFS_MAGIC)),
1402+
.version = lcfs_u32_to_file(((uint32_t)LCFS_EROFS_VERSION)),
14031403
.composefs_version = lcfs_u32_to_file(ctx->options->version),
14041404
};
14051405
uint32_t header_flags;
14061406
struct erofs_super_block superblock = {
1407-
.magic = lcfs_u32_to_file(EROFS_SUPER_MAGIC_V1),
1407+
.magic = lcfs_u32_to_file(((uint32_t)EROFS_SUPER_MAGIC_V1)),
14081408
.blkszbits = EROFS_BLKSIZ_BITS,
14091409
};
14101410
int ret = 0;
@@ -1447,11 +1447,12 @@ int lcfs_write_erofs_to(struct lcfs_ctx_s *ctx)
14471447
if (ret < 0)
14481448
return ret;
14491449

1450-
superblock.feature_compat = lcfs_u32_to_file(
1451-
EROFS_FEATURE_COMPAT_MTIME | EROFS_FEATURE_COMPAT_XATTR_FILTER);
1450+
superblock.feature_compat =
1451+
lcfs_u32_to_file((uint32_t)(EROFS_FEATURE_COMPAT_MTIME |
1452+
EROFS_FEATURE_COMPAT_XATTR_FILTER));
14521453
superblock.inos = lcfs_u64_to_file(ctx->num_inodes);
14531454

1454-
superblock.build_time = lcfs_u64_to_file(ctx->min_mtim_sec);
1455+
superblock.build_time = lcfs_u64_to_file((uint64_t)ctx->min_mtim_sec);
14551456
superblock.build_time_nsec = lcfs_u32_to_file(ctx->min_mtim_nsec);
14561457

14571458
/* metadata is stored directly after superblock */

0 commit comments

Comments
 (0)