Skip to content
Tcl Source Code View Ticket
Login

View Ticket

avoid misaligned pointers in macOS file attribute functions

Closed / Fixed992f94d847abfc48145f375561505932bf509604Created 5 years agoUpdated 1 year ago
Descriptionwiki2021-12-11 00:17:39
In TclMacOSXGetFileAttribute() and TclMacOSXSetFileAttribute() (macosx/tclMacOSXFCmd.c), fileinfobuf finfo only has 4-byte alignment. When finfo.data is not 8-byte aligned, casting &finfo.data to (*off_t) is undefined behavior, and UBSan (-fsanitize=alignment) will complain when off_t *rsrcForkSize is dereferenced. Output for macOSXFCmd.test (with macOSXFCmd-2.7 reenabled, which I observe passes on APFS):
$ ./tcltest ../tests/macOSXFCmd.test
tcl/macosx/tclMacOSXFCmd.c:199:39: runtime error: load of misaligned address 0x7ff7bf70a1d4 for type 'off_t' (aka 'long long'), which requires 8 byte alignment
0x7ff7bf70a1d4: note: pointer points here
  0c 00 00 00 00 00 00 00  00 00 00 00 0f 00 00 00  10 6d fe 01 01 00 00 00  80 36 5b 00 00 60 00 00
              ^ 
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/macosx/tclMacOSXFCmd.c:199:39 in 
tcl/macosx/tclMacOSXFCmd.c:327:25: runtime error: load of misaligned address 0x7ff7bf70a0f4 for type 'off_t' (aka 'long long'), which requires 8 byte alignment
0x7ff7bf70a0f4: note: pointer points here
  0c 00 00 00 03 00 00 00  00 00 00 00 00 60 00 00  00 42 ca 02 00 60 00 00  68 42 ca 02 00 60 00 00
              ^ 
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/macosx/tclMacOSXFCmd.c:327:25 in 
macOSXFCmd.test:	Total	14	Passed	14	Skipped	0	Failed	0

The attached patch uses memcpy() to avoid creating or dereferencing misaligned pointers, although there may be a better approach.

Referenced by2
  • 6badbfd55eFix [992f94d847]: avoid misaligned pointers in macOS file attribute functions check-in2025-08-12 21:51:30
  • d53a634a93Alternative proposed fix for [992f94d847]: avoid misaligned pointers in macOS file attribute functions check-in2025-08-12 10:03:34
chrstphrchvz Ticket [992f94d847]: 2 changes [History diff]2022-02-16 23:17:24
jan.nijtmanswiki2025-08-12 10:07:09

Another possible approach: just making sure that "data" is 8-byte allighed:

[d53a634a93ec36ca]

How do you think about that?

chrstphrchvzwiki2025-08-12 17:55:50

That also seems to work, thanks

jan.nijtmanswiki2025-08-12 22:11:47

Fixed [here]

Log in (anonymous is fine) to add a comment.