Tcl Source Code

View Ticket
Login
Ticket UUID: 992f94d847abfc48145f375561505932bf509604
Title: avoid misaligned pointers in macOS file attribute functions
Type: Patch Created on: 2021-12-11 00:17:39
Submitter: chrstphrchvz Assigned to: jan.nijtmans
Subsystem: 37. File System Severity: Minor
Priority: 5 Medium Last modified: 2025-08-12 22:11:47
Status: Closed Closed by: jan.nijtmans
Resolution: Fixed Closed on: 2025-08-12 22:11:47
Version: 8.6.12
Description:

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.

User Comments:
jan.nijtmans added on 2025-08-12 10:07:09:

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

[d53a634a93ec36ca]

How do you think about that?


chrstphrchvz added on 2025-08-12 17:55:50:

That also seems to work, thanks


jan.nijtmans added on 2025-08-12 22:11:47:

Fixed [6badbfd55e096dac|here]


Attachments: