msg_135.c revision 1.13 1 1.13 rillig /* $NetBSD: msg_135.c,v 1.13 2023/04/22 19:45:04 rillig Exp $ */
2 1.1 rillig # 3 "msg_135.c"
3 1.1 rillig
4 1.11 rillig // Test for message: converting '%s' to '%s' increases alignment from %u to %u [135]
5 1.1 rillig
6 1.12 rillig /* lint1-extra-flags: -h -X 351 */
7 1.3 rillig
8 1.6 rillig void sink(const void *);
9 1.6 rillig
10 1.3 rillig unsigned
11 1.9 rillig read_uint(const unsigned short **pp)
12 1.3 rillig {
13 1.3 rillig unsigned val;
14 1.3 rillig
15 1.11 rillig /* expect+1: warning: converting 'pointer to const unsigned short' to 'pointer to const unsigned int' increases alignment from 2 to 4 [135] */
16 1.9 rillig val = *(const unsigned *)(*pp);
17 1.3 rillig pp += sizeof(unsigned);
18 1.3 rillig return val;
19 1.3 rillig }
20 1.6 rillig
21 1.10 rillig /* expect+1: warning: struct 'incomplete' never defined [233] */
22 1.10 rillig struct incomplete;
23 1.6 rillig
24 1.6 rillig struct complete {
25 1.13 rillig int member;
26 1.6 rillig };
27 1.6 rillig
28 1.6 rillig /*
29 1.6 rillig * These types of conversions are typically seen in OpenSSL, when converting
30 1.6 rillig * from the publicly visible, incomplete 'struct lhash_st' to a private
31 1.6 rillig * implementation type such as 'struct lhash_st_OPENSSL_STRING'.
32 1.6 rillig *
33 1.7 rillig * Before tree.c 1.277 from 2021-04-17, lint warned about this, even though
34 1.7 rillig * there was not enough evidence that there really was an alignment problem,
35 1.7 rillig * resulting in many false positives.
36 1.7 rillig *
37 1.6 rillig * See openssl/lhash.h.
38 1.6 rillig */
39 1.6 rillig void
40 1.6 rillig pointer_to_structs(struct incomplete *incomplete)
41 1.6 rillig {
42 1.6 rillig struct complete *complete;
43 1.6 rillig
44 1.7 rillig complete = (struct complete *)incomplete;
45 1.6 rillig sink(complete);
46 1.6 rillig }
47 1.8 rillig
48 1.9 rillig /*
49 1.9 rillig * Before tree.c 1.316 from 2021-07-15, lint warned about pointer casts from
50 1.9 rillig * unsigned char or plain char to another type. These casts often occur in
51 1.9 rillig * traditional code that does not use void pointers, even 30 years after C90
52 1.9 rillig * introduced 'void'.
53 1.9 rillig */
54 1.8 rillig void
55 1.8 rillig unsigned_char_to_unsigned_type(unsigned char *ucp)
56 1.8 rillig {
57 1.8 rillig unsigned short *usp;
58 1.8 rillig
59 1.8 rillig usp = (unsigned short *)ucp;
60 1.8 rillig sink(usp);
61 1.8 rillig }
62 1.8 rillig
63 1.9 rillig /*
64 1.9 rillig * Before tree.c 1.316 from 2021-07-15, lint warned about pointer casts from
65 1.9 rillig * unsigned char or plain char to another type. These casts often occur in
66 1.9 rillig * traditional code that does not use void pointers, even 30 years after C90
67 1.9 rillig * introduced 'void'.
68 1.9 rillig */
69 1.8 rillig void
70 1.8 rillig plain_char_to_unsigned_type(char *cp)
71 1.8 rillig {
72 1.8 rillig unsigned short *usp;
73 1.8 rillig
74 1.8 rillig usp = (unsigned short *)cp;
75 1.8 rillig sink(usp);
76 1.8 rillig }
77 1.13 rillig
78 1.13 rillig /*
79 1.13 rillig * Converting a pointer with a low alignment requirement to a union that
80 1.13 rillig * includes other types with higher alignment requirements is safe. While
81 1.13 rillig * accessing any other member of the union might trigger an alignment
82 1.13 rillig * violation, such an access would invoke undefined behavior anyway.
83 1.13 rillig *
84 1.13 rillig * A practical case for this pattern are tagged unions, in which the first
85 1.13 rillig * member of the struct determines how the remaining members are interpreted.
86 1.13 rillig * See sbin/newfs_udf, function udf_validate_tag_and_crc_sums for an example.
87 1.13 rillig */
88 1.13 rillig double
89 1.13 rillig cast_to_union(void)
90 1.13 rillig {
91 1.13 rillig int align_4 = 0;
92 1.13 rillig double align_8 = 0.0;
93 1.13 rillig union both {
94 1.13 rillig int p_align_4;
95 1.13 rillig double p_align_8;
96 1.13 rillig } *both;
97 1.13 rillig
98 1.13 rillig /* TODO: don't warn here. */
99 1.13 rillig /* expect+1: warning: converting 'pointer to int' to 'pointer to union both' increases alignment from 4 to 8 [135] */
100 1.13 rillig both = (union both *)&align_4;
101 1.13 rillig both = (union both *)&align_8;
102 1.13 rillig return both->p_align_8;
103 1.13 rillig }
104