refactor: replace byte-swap macros with static inline functions#618
Open
hugo-hur wants to merge 1 commit into
Open
refactor: replace byte-swap macros with static inline functions#618hugo-hur wants to merge 1 commit into
hugo-hur wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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
Type of change
Checklist: