Home | History | Annotate | Line # | Download | only in lint1
      1 /*	$NetBSD: msg_247.c,v 1.35 2025/04/12 15:49:50 rillig Exp $	*/
      2 # 3 "msg_247.c"
      3 
      4 // Test for message: pointer cast from '%s' to unrelated '%s' [247]
      5 
      6 //
      7 // The word 'may' in the message text means that the trouble is not necessarily
      8 // on this platform with its specific type sizes, but on other platforms.
      9 //
     10 // See also:
     11 //	msg_247_ilp32_ldbl64.c
     12 //	msg_247_lp64_ldbl128.c
     13 //	msg_247_portable.c
     14 //	msg_247_portable_int.c
     15 
     16 /* lint1-extra-flags: -c -X 351 */
     17 
     18 /* example taken from Xlib.h */
     19 typedef struct {
     20 	int id;
     21 } *PDisplay;
     22 
     23 struct Other {
     24 	int id;
     25 };
     26 
     27 PDisplay
     28 example(struct Other *arg)
     29 {
     30 	/*
     31 	 * Before tree.c 1.461 from 2022-06-24, lint warned about the cast
     32 	 * between the structs.
     33 	 *
     34 	 * XXX: The target type was reported as 'struct <unnamed>'.  In cases
     35 	 *  like these, it would be helpful to print at least the type name
     36 	 *  of the pointer.  This type name though is discarded immediately
     37 	 *  in the grammar rule 'typespec: T_TYPENAME'.
     38 	 *  After that, the target type of the cast is just an unnamed struct,
     39 	 *  with no hint at all that there is a typedef for a pointer to the
     40 	 *  struct.
     41 	 */
     42 	return (PDisplay)arg;
     43 }
     44 
     45 /*
     46  * C code with a long history that has existed in pre-C90 times already often
     47  * uses 'pointer to char' where modern code would use 'pointer to void'.
     48  * Since 'char' is the most general underlying type, there is nothing wrong
     49  * with casting to it.  An example for this type of code is X11.
     50  *
     51  * Casting to 'pointer to char' may also be used by programmers who don't know
     52  * about endianness, but that's not something lint can do anything about.  The
     53  * code for these two use cases looks exactly the same, so lint errs on the
     54  * side of fewer false positive warnings here.
     55  */
     56 char *
     57 cast_to_char_pointer(struct Other *arg)
     58 {
     59 	return (char *)arg;
     60 }
     61 
     62 /*
     63  * In traditional C, there was 'unsigned char' as well, so the same reasoning
     64  * as for plain 'char' applies here.
     65  */
     66 unsigned char *
     67 cast_to_unsigned_char_pointer(struct Other *arg)
     68 {
     69 	return (unsigned char *)arg;
     70 }
     71 
     72 /*
     73  * Traditional C does not have the type specifier 'signed', which means that
     74  * this type cannot be used by old code.  Therefore warn about this.  All code
     75  * that triggers this warning should do the intermediate cast via 'void
     76  * pointer'.
     77  */
     78 signed char *
     79 cast_to_signed_char_pointer(struct Other *arg)
     80 {
     81 	/* expect+1: warning: pointer cast from 'struct Other' to unrelated 'signed char' [247] */
     82 	return (signed char *)arg;
     83 }
     84 
     85 char *
     86 cast_to_void_pointer_then_to_char_pointer(struct Other *arg)
     87 {
     88 	return (char *)(void *)arg;
     89 }
     90 
     91 
     92 /*
     93  * When implementing types that have a public part that is exposed to the user
     94  * (in this case 'struct counter') and a private part that is only visible to
     95  * the implementation (in this case 'struct counter_impl'), a common
     96  * implementation technique is to use a struct in which the public part is the
     97  * first member.  C guarantees that the pointer to the first member is at the
     98  * same address as the pointer to the whole struct.
     99  *
    100  * Seen in external/mpl/bind/dist/lib/isc/mem.c for 'struct isc_mem' and
    101  * 'struct isc__mem'.
    102  */
    103 
    104 struct counter {
    105 	int count;
    106 };
    107 
    108 struct counter_impl {
    109 	struct counter public_part;
    110 	int saved_count;
    111 };
    112 
    113 void *allocate(void);
    114 
    115 struct counter *
    116 counter_new_typesafe(void)
    117 {
    118 	struct counter_impl *impl = allocate();
    119 	impl->public_part.count = 12345;
    120 	impl->saved_count = 12346;
    121 	return &impl->public_part;
    122 }
    123 
    124 struct counter *
    125 counter_new_cast(void)
    126 {
    127 	struct counter_impl *impl = allocate();
    128 	impl->public_part.count = 12345;
    129 	impl->saved_count = 12346;
    130 	/* Before tree.c 1.462 from 2022-06-24, lint warned about this cast. */
    131 	return (struct counter *)impl;
    132 }
    133 
    134 void
    135 counter_increment(struct counter *counter)
    136 {
    137 	/*
    138 	 * Before tree.c 1.272 from 2021-04-08, lint warned about the cast
    139 	 * from 'struct counter' to 'struct counter_impl'.
    140 	 */
    141 	struct counter_impl *impl = (struct counter_impl *)counter;
    142 	impl->saved_count = impl->public_part.count;
    143 	impl->public_part.count++;
    144 }
    145 
    146 
    147 /*
    148  * In OpenSSL, the hashing API uses the incomplete 'struct lhash_st' for their
    149  * type-generic hashing API while defining a separate struct for each type to
    150  * be hashed.
    151  *
    152  * Before 2021-04-09, in a typical NetBSD build this led to about 38,000 lint
    153  * warnings about possibly troublesome pointer casts.
    154  */
    155 
    156 /* expect+1: warning: struct 'lhash_st' never defined [233] */
    157 struct lhash_st;
    158 
    159 struct lhash_st *OPENSSL_LH_new(void);
    160 
    161 struct lhash_st_OPENSSL_STRING {
    162 	union lh_OPENSSL_STRING_dummy {
    163 		void *d1;
    164 		unsigned long d2;
    165 		int d3;
    166 	} dummy;
    167 };
    168 
    169 # 196 "lhash.h" 1 3 4
    170 struct lhash_st_OPENSSL_STRING *
    171 lh_OPENSSL_STRING_new(void)
    172 {
    173 	/*
    174 	 * Since tree.c 1.274 from 2021-04-09, lint does not warn about casts
    175 	 * to or from incomplete structs anymore.
    176 	 */
    177 	return (struct lhash_st_OPENSSL_STRING *)OPENSSL_LH_new();
    178 }
    179 # 180 "msg_247.c" 2
    180 
    181 void sink(const void *);
    182 
    183 /*
    184  * Before tree.c 1.316 from 2021-07-15, lint warned about pointer casts from
    185  * unsigned char or plain char to another type.  These casts often occur in
    186  * traditional code that does not use void pointers, even 30 years after C90
    187  * introduced 'void'.
    188  */
    189 void
    190 unsigned_char_to_unsigned_type(unsigned char *ucp)
    191 {
    192 	unsigned short *usp;
    193 
    194 	usp = (unsigned short *)ucp;
    195 	sink(usp);
    196 }
    197 
    198 /*
    199  * Before tree.c 1.316 from 2021-07-15, lint warned about pointer casts from
    200  * unsigned char or plain char to another type.  These casts often occur in
    201  * traditional code that does not use void pointers, even 30 years after C90
    202  * introduced 'void'.
    203  */
    204 void
    205 plain_char_to_unsigned_type(char *cp)
    206 {
    207 	unsigned short *usp;
    208 
    209 	usp = (unsigned short *)cp;
    210 	sink(usp);
    211 }
    212 
    213 /*
    214  * Before tree.c 1.460 from 2022-06-24, lint warned about pointer casts from
    215  * unsigned char or plain char to a struct or union type.  These casts often
    216  * occur in traditional code that does not use void pointers, even 30 years
    217  * after C90 introduced 'void'.
    218  */
    219 void
    220 char_to_struct(void *ptr)
    221 {
    222 
    223 	sink((struct counter *)(char *)ptr);
    224 
    225 	sink((struct counter *)(unsigned char *)ptr);
    226 
    227 	/* expect+1: warning: pointer cast from 'signed char' to unrelated 'struct counter' [247] */
    228 	sink((struct counter *)(signed char *)ptr);
    229 }
    230 
    231 
    232 // The following data types are simplified from various system headers.
    233 
    234 typedef unsigned char uint8_t;
    235 typedef unsigned short uint16_t;
    236 typedef unsigned int uint32_t;
    237 
    238 typedef uint16_t in_port_t;
    239 typedef uint8_t sa_family_t;
    240 
    241 struct sockaddr {
    242 	uint8_t sa_len;
    243 	sa_family_t sa_family;
    244 	char sa_data[14];
    245 };
    246 
    247 struct in_addr {
    248 	uint32_t s_addr;
    249 };
    250 
    251 struct sockaddr_in {
    252 	uint8_t sin_len;
    253 	sa_family_t sin_family;
    254 	in_port_t sin_port;
    255 	struct in_addr sin_addr;
    256 	uint8_t sin_zero[8];
    257 };
    258 
    259 struct sockaddr_in6 {
    260 	uint8_t sin6_len;
    261 	sa_family_t sin6_family;
    262 	in_port_t sin6_port;
    263 	uint32_t sin6_flowinfo;
    264 	union {
    265 		uint8_t u6_addr8[16];
    266 		uint16_t u6_addr16[8];
    267 		uint32_t u6_addr32[4];
    268 	} sin6_addr;
    269 	uint32_t sin6_scope_id;
    270 };
    271 
    272 /*
    273  * Before tree.c 1.461 from 2022-06-24, lint warned about the cast between the
    274  * sockaddr variants.  Since then, lint allows casts between pointers to
    275  * structs if the initial members have compatible types and either of the
    276  * struct types continues with a byte array.
    277  */
    278 void *
    279 cast_between_sockaddr_variants(void *ptr)
    280 {
    281 
    282 	void *t1 = (struct sockaddr_in *)(struct sockaddr *)ptr;
    283 	void *t2 = (struct sockaddr *)(struct sockaddr_in *)t1;
    284 	void *t3 = (struct sockaddr_in6 *)(struct sockaddr *)t2;
    285 	void *t4 = (struct sockaddr *)(struct sockaddr_in6 *)t3;
    286 
    287 	/* expect+1: warning: pointer cast from 'struct sockaddr_in6' to unrelated 'struct sockaddr_in' [247] */
    288 	void *t5 = (struct sockaddr_in *)(struct sockaddr_in6 *)t4;
    289 
    290 	/* expect+1: warning: pointer cast from 'struct sockaddr_in' to unrelated 'struct sockaddr_in6' [247] */
    291 	void *t6 = (struct sockaddr_in6 *)(struct sockaddr_in *)t5;
    292 
    293 	return t6;
    294 }
    295 
    296 
    297 // From jemalloc.
    298 
    299 typedef struct ctl_node_s {
    300 	_Bool named;
    301 } ctl_node_t;
    302 
    303 typedef struct ctl_named_node_s {
    304 	ctl_node_t node;
    305 	const char *name;
    306 } ctl_named_node_t;
    307 
    308 void *
    309 cast_between_first_member_struct(void *ptr)
    310 {
    311 	/* Before tree.c 1.462 from 2022-06-24, lint warned about this cast. */
    312 	/* expect+1: warning: 't1' set but not used in function 'cast_between_first_member_struct' [191] */
    313 	void *t1 = (ctl_node_t *)(ctl_named_node_t *)ptr;
    314 
    315 	void *t2 = (ctl_named_node_t *)(ctl_node_t *)ptr;
    316 
    317 	return t2;
    318 }
    319 
    320 double *
    321 unnecessary_cast_from_array_to_pointer(int dim)
    322 {
    323 	static double storage_1d[10];
    324 	static double storage_2d[10][5];
    325 
    326 	if (dim == 1)
    327 		return (double *)storage_1d;
    328 
    329 	if (dim == -1)
    330 		return storage_1d;
    331 
    332 	if (dim == 2)
    333 		/* expect+1: warning: invalid combination of 'pointer to double' and 'pointer to array[5] of double' [184] */
    334 		return storage_2d;
    335 
    336 	/*
    337 	 * C11 6.3.2.1p3 says that an array is converted to a pointer to its
    338 	 * first element.  That paragraph doesn't say 'recursively', that
    339 	 * word is only used two paragraphs above, in 6.3.2.1p1.
    340 	 */
    341 	if (dim == -2)
    342 		return storage_2d[0];
    343 
    344 	return (double *)storage_2d;
    345 }
    346 
    347 
    348 typedef void (*function)(void);
    349 
    350 typedef struct {
    351 	function m_function_array[5];
    352 } struct_function_array;
    353 
    354 typedef union {
    355 	int um_int;
    356 	double um_double;
    357 	struct_function_array um_function_array;
    358 } anything;
    359 
    360 static int *p_int;
    361 static double *p_double;
    362 static function p_function;
    363 static struct_function_array *p_function_array;
    364 static anything *p_anything;
    365 
    366 void
    367 conversions_from_and_to_union(void)
    368 {
    369 	/* Self-assignment, disguised by a cast to its own type. */
    370 	p_int = (int *)p_int;
    371 	/* Self-assignment, disguised by a cast to a pointer. */
    372 	p_int = (void *)p_int;
    373 
    374 	/* expect+1: warning: invalid combination of 'pointer to int' and 'pointer to double', op '=' [124] */
    375 	p_int = p_double;
    376 	/* expect+1: warning: pointer cast from 'double' to unrelated 'int' [247] */
    377 	p_int = (int *)p_double;
    378 
    379 	/* expect+1: warning: invalid combination of 'pointer to union typedef anything' and 'pointer to double', op '=' [124] */
    380 	p_anything = p_double;
    381 	/* OK, since the union 'anything' has a 'double' member. */
    382 	p_anything = (anything *)p_double;
    383 	/* expect+1: warning: invalid combination of 'pointer to double' and 'pointer to union typedef anything', op '=' [124] */
    384 	p_double = p_anything;
    385 	/* OK, since the union 'anything' has a 'double' member. */
    386 	p_double = (double *)p_anything;
    387 
    388 	/*
    389 	 * Casting to an intermediate union does not make casting between two
    390 	 * incompatible types better.
    391 	 */
    392 	/* expect+1: warning: invalid combination of 'pointer to function(void) returning void' and 'pointer to union typedef anything', op '=' [124] */
    393 	p_function = (anything *)p_int;
    394 
    395 	/* expect+2: warning: converting 'pointer to function(void) returning void' to 'pointer to union typedef anything' is questionable [229] */
    396 	/* expect+1: warning: invalid combination of 'pointer to function(void) returning void' and 'pointer to union typedef anything', op '=' [124] */
    397 	p_function = (anything *)p_function_array->m_function_array[0];
    398 
    399 	/* expect+1: warning: invalid combination of 'pointer to int' and 'pointer to function(void) returning void', op '=' [124] */
    400 	p_int = p_function;
    401 }
    402