Page MenuHomeFreeBSD

rup: Fix -Wcast-align warnings
ClosedPublic

Authored by jilles on Jun 30 2018, 10:49 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sat, Jan 11, 5:47 PM
Unknown Object (File)
Dec 11 2024, 3:21 PM
Unknown Object (File)
Nov 25 2024, 2:29 AM
Unknown Object (File)
Nov 11 2024, 5:51 PM
Unknown Object (File)
Oct 22 2024, 8:52 PM
Unknown Object (File)
Oct 3 2024, 11:43 AM
Unknown Object (File)
Oct 3 2024, 11:03 AM
Unknown Object (File)
Oct 2 2024, 9:45 PM
Subscribers

Details

Summary

Fix possible strict aliasing issue (if time_t is the same size as int but
not int but for example long) which also resulted in a false positive
warning on systems with 64-bit time_t. Pointer casts are bad, we can just
copy the time_t.

Elsewhere, avoid casting char * to int * by using memcpy().

Test Plan

buildworld on various architectures
runtime test of amd64 and i386 binary on amd64 kernel

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Thanks!

For my edification: the two possible solutions to cast alignment are (a) memcpy and (b) aligned_alloc. The issue with the latter is that the compiler likely won't understand it, and thus still warn.

Am I missing anything else?

This revision is now accepted and ready to land.Jun 30 2018, 1:54 PM

For my edification: the two possible solutions to cast alignment are (a) memcpy and (b) aligned_alloc. The issue with the latter is that the compiler likely won't understand it, and thus still warn.

It is not necessary to use aligned_alloc() to get storage for standard data types sufficiently aligned. The specification for malloc() already guarantees this. (However, SIMD extensions such as SSE and AVX may require or benefit from more than standard alignment and tricky people may want to increase alignment to get additional bits in pointers to use for other purposes.)

Two common solutions to -Wcast-align issues are memcpy and converting the data pointed to by pointers instead of the pointers. These fixes also help with strict-aliasing violations.

Situations like p = malloc(sizeof(struct foo) + sizeof(struct bar)); may result in alignment problems without aliasing. In simple cases, this can be avoided by allocating space for a struct foobar { struct foo foo; struct bar bar; } but in more complicated cases with multiple arrays this requires manual alignment fixups and suppressing -Wcast-align in some way.

This revision was automatically updated to reflect the committed changes.