Skip to content

refactor: replace byte-swap macros with static inline functions#618

Open
hugo-hur wants to merge 1 commit into
LinearTapeFileSystem:mainfrom
hugo-hur:refactor-endian-inline
Open

refactor: replace byte-swap macros with static inline functions#618
hugo-hur wants to merge 1 commit into
LinearTapeFileSystem:mainfrom
hugo-hur:refactor-endian-inline

Conversation

@hugo-hur

Copy link
Copy Markdown

The ltfs_betou*/ltfs_u*tobe macros cast byte buffers to uint16_t/uint32_t and dereferenced them, e.g. *(uint32_t *)(buf). Callers pass buffers at arbitrary offsets (cdb + 3, coh_data + 6), so these were unaligned accesses and causes undefined behaviour that happens to work on x86 but faults or returns rotated/garbage data on alignment-strict CPUs. Convert each to a static inline function that moves bytes through memcpy, which is well defined for any alignment and lowers to a single load/store plus byte swap without runtime cost.

Using functions instead of macros also:

  • Adds real type checking: parameters and return values have concrete types (void */const void * buffers, fixed-width integers) instead of textual substitution, so passing the wrong type is caught at compile time rather than silently reinterpreted.
  • Produces clearer compiler errors: diagnostics point at the call site and the function signature instead of deep inside a macro expansion.
  • Evaluates each argument exactly once and needs no defensive parentheses, removing the double-evaluation and operator-precedence hazards that the old ltfs_betou64 macro had.

The signatures are unchanged, so all call sites are untouched. Add <stdint.h> and <string.h> so the header is self-contained.

Summary of changes

  • Change byte-swap endianness macros into static inline functions

Type of change

  • Refactoring

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings

The ltfs_betou*/ltfs_u*tobe macros cast byte buffers to uint16_t/uint32_t
and dereferenced them, e.g. *(uint32_t *)(buf). Callers pass buffers at
arbitrary offsets (cdb + 3, coh_data + 6), so these were unaligned accesses
-- undefined behaviour that happens to work on x86 but faults (SIGBUS) or
returns rotated/garbage data on alignment-strict CPUs, and trips UBSan.
Convert each to a static inline function that moves bytes through memcpy,
which is well defined for any alignment and lowers to a single load/store
plus byte swap, so there is no runtime cost.

Using functions instead of macros also:

- Adds real type checking: parameters and return values have concrete
  types (void */const void * buffers, fixed-width integers) instead of
  textual substitution, so passing the wrong type is caught at compile
  time rather than silently reinterpreted.
- Produces clearer compiler errors: diagnostics point at the call site and
  the function signature instead of deep inside a macro expansion.
- Evaluates each argument exactly once and needs no defensive parentheses,
  removing the double-evaluation and operator-precedence hazards that the
  old ltfs_betou64 macro had.

The signatures are unchanged, so all call sites are untouched. Add
<stdint.h> and <string.h> so the header is self-contained.
@vandelvan vandelvan requested a review from madjesc June 15, 2026 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant