3 ms·
Pointers correctly round-trip through unrelated pointer types, provided the alignment for both types is compatible. At least one bit does look a bit questionab
by tom_ 3y ago
Pointers correctly round-trip through unrelated pointer types, provided the alignment for both types is compatible.
At least one bit does look a bit questionable: lfs_mlist is treated as the common initial sequence of lfs_dir and lfs_file, even though it isn't, and common initial sequences only apply to union fields anyway. Example cast of struct dir * to struct lfs_mlist * (probably valid of itself, assuming the alignment is compatible): https://github.com/littlefs-project/littlefs/blob/c733d9ec5776dfc949ec6dc71fa9ce3ff71de6e5/lfs.c#L2663 https://github.com/littlefs-project/littlefs/blob/c733d9ec57...; then use struct dir as if it were actually a struct lfs_mlist: https://github.com/littlefs-project/littlefs/blob/c733d9ec5776dfc949ec6dc71fa9ce3ff71de6e5/lfs.c#L516 https://github.com/littlefs-project/littlefs/blob/c733d9ec57...
(There's other occurrences of the same kind of thing.)
Strictly speaking, I think this might be unfixable without a bunch of work, but so much stuff does this kind of operation that any compiler that doesn't do what you expect will have been fixed by now. (Assuming you're not one of those people who is going to pop up and tell us with a straight face that what we should expect is for the compiler to do absolutely anything - except, perhaps, for having it generate correct code, which would be defective behaviour that should be eliminated.) Maybe improve the odds by using lfs_mlist as the common initial sequence of both structs, and fingers crossed that the compiler considers the union rules to apply to this case too. Or compile with -fno-strict-aliasing.