Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2025-08-12
22:11 • Closed ticket [992f94d847]: avoid misaligned pointers in macOS file attribute fu... artifact: dcf23c1260 user: jan.nijtmans
21:51
Fix [992f94d847]: avoid misaligned pointers in macOS file attribute functions check-in: 6badbfd55e user: jan.nijtmans tags: core-8-6-branch
17:55 • Ticket [992f94d847] avoid misaligned pointers in macOS file attribute functions<... artifact: b4b42288a3 user: chrstphrchvz
10:07 • Ticket [992f94d847]: 3 changes artifact: 639f80db0d user: jan.nijtmans
10:03
Alternative proposed fix for [992f94d847]: avoid misaligned pointers in macOS file attribute functio... closed check-in: d53a634a93 user: jan.nijtmans tags: bug-992f94d847
2022-02-16
23:17 • Ticket [992f94d847] avoid misaligned pointers in macOS file attribute functions<... artifact: cd24f5c690 user: chrstphrchvz
2021-12-11
00:21 • Add attachment 992f94d847.diff to ticket [992f94d847] artifact: e8aab0c309 user: chrstphrchvz
00:17 • New ticket [992f94d847] avoid misaligned pointers in macOS file attribute functi... artifact: 23ed893984 user: chrstphrchvz

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.