Home | History | Annotate | Line # | Download | only in lint1
msg_132.c revision 1.47
      1 /*	$NetBSD: msg_132.c,v 1.47 2025/01/01 14:03:42 rillig Exp $	*/
      2 # 3 "msg_132.c"
      3 
      4 // Test for message: conversion from '%s' to '%s' may lose accuracy [132]
      5 
      6 /* lint1-extra-flags: -X 351 */
      7 
      8 /*
      9  * NetBSD's default lint flags only include a single -a, which only flags
     10  * narrowing conversions from long.  To get warnings for all narrowing
     11  * conversions, -a needs to be given more than once.
     12  *
     13  * https://gnats.netbsd.org/14531
     14  */
     15 
     16 /* lint1-extra-flags: -aa */
     17 
     18 typedef unsigned char u8_t;
     19 typedef unsigned short u16_t;
     20 typedef unsigned int u32_t;
     21 typedef unsigned long long u64_t;
     22 typedef signed char s8_t;
     23 typedef signed short s16_t;
     24 typedef signed int s32_t;
     25 typedef signed long long s64_t;
     26 
     27 _Bool cond;
     28 char ch;
     29 
     30 u8_t u8;
     31 u16_t u16;
     32 u32_t u32;
     33 u64_t u64;
     34 
     35 s8_t s8;
     36 s16_t s16;
     37 s32_t s32;
     38 s64_t s64;
     39 
     40 struct bit_fields {
     41 	unsigned u1:1;
     42 	unsigned u2:2;
     43 	unsigned u3:3;
     44 	unsigned u4:4;
     45 	unsigned u5:5;
     46 	unsigned u6:6;
     47 	unsigned u7:7;
     48 	unsigned u8:8;
     49 	unsigned u9:9;
     50 	unsigned u10:10;
     51 	unsigned u11:11;
     52 	unsigned u12:12;
     53 	unsigned u32:32;
     54 } bits;
     55 
     56 
     57 void
     58 unsigned_to_unsigned(void)
     59 {
     60 	/* expect+1: warning: conversion from 'unsigned short' to 'unsigned char' may lose accuracy [132] */
     61 	u8 = u16;
     62 	/* expect+1: warning: conversion from 'unsigned int' to 'unsigned char' may lose accuracy [132] */
     63 	u8 = u32;
     64 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned char' may lose accuracy [132] */
     65 	u8 = u64;
     66 
     67 	u16 = u8;
     68 	/* expect+1: warning: conversion from 'unsigned int' to 'unsigned short' may lose accuracy [132] */
     69 	u16 = u32;
     70 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned short' may lose accuracy [132] */
     71 	u16 = u64;
     72 
     73 	u32 = u8;
     74 	u32 = u16;
     75 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132] */
     76 	u32 = u64;
     77 
     78 	u64 = u8;
     79 	u64 = u16;
     80 	u64 = u32;
     81 }
     82 
     83 void
     84 unsigned_to_signed(void)
     85 {
     86 	/* expect+1: warning: conversion from 'unsigned short' to 'signed char' may lose accuracy [132] */
     87 	s8 = u16;
     88 	/* expect+1: warning: conversion from 'unsigned int' to 'signed char' may lose accuracy [132] */
     89 	s8 = u32;
     90 	/* expect+1: warning: conversion from 'unsigned long long' to 'signed char' may lose accuracy [132] */
     91 	s8 = u64;
     92 
     93 	s16 = u8;
     94 	/* expect+1: warning: conversion from 'unsigned int' to 'short' may lose accuracy [132] */
     95 	s16 = u32;
     96 	/* expect+1: warning: conversion from 'unsigned long long' to 'short' may lose accuracy [132] */
     97 	s16 = u64;
     98 
     99 	s32 = u8;
    100 	s32 = u16;
    101 	/* expect+1: warning: conversion from 'unsigned long long' to 'int' may lose accuracy [132] */
    102 	s32 = u64;
    103 
    104 	s64 = u8;
    105 	s64 = u16;
    106 	s64 = u32;
    107 }
    108 
    109 void
    110 signed_to_unsigned(void)
    111 {
    112 	/* expect+1: warning: conversion from 'short' to 'unsigned char' may lose accuracy [132] */
    113 	u8 = s16;
    114 	/* expect+1: warning: conversion from 'int' to 'unsigned char' may lose accuracy [132] */
    115 	u8 = s32;
    116 	/* expect+1: warning: conversion from 'long long' to 'unsigned char' may lose accuracy [132] */
    117 	u8 = s64;
    118 
    119 	u16 = s8;
    120 	/* expect+1: warning: conversion from 'int' to 'unsigned short' may lose accuracy [132] */
    121 	u16 = s32;
    122 	/* expect+1: warning: conversion from 'long long' to 'unsigned short' may lose accuracy [132] */
    123 	u16 = s64;
    124 
    125 	u32 = s8;
    126 	u32 = s16;
    127 	/* expect+1: warning: conversion from 'long long' to 'unsigned int' may lose accuracy [132] */
    128 	u32 = s64;
    129 
    130 	u64 = s8;
    131 	u64 = s16;
    132 	u64 = s32;
    133 }
    134 
    135 void
    136 signed_to_signed(void)
    137 {
    138 	/* expect+1: warning: conversion from 'short' to 'signed char' may lose accuracy [132] */
    139 	s8 = s16;
    140 	/* expect+1: warning: conversion from 'int' to 'signed char' may lose accuracy [132] */
    141 	s8 = s32;
    142 	/* expect+1: warning: conversion from 'long long' to 'signed char' may lose accuracy [132] */
    143 	s8 = s64;
    144 
    145 	s16 = s8;
    146 	/* expect+1: warning: conversion from 'int' to 'short' may lose accuracy [132] */
    147 	s16 = s32;
    148 	/* expect+1: warning: conversion from 'long long' to 'short' may lose accuracy [132] */
    149 	s16 = s64;
    150 
    151 	s32 = s8;
    152 	s32 = s16;
    153 	/* expect+1: warning: conversion from 'long long' to 'int' may lose accuracy [132] */
    154 	s32 = s64;
    155 
    156 	s64 = s8;
    157 	s64 = s16;
    158 	s64 = s32;
    159 }
    160 
    161 /*
    162  * Before tree.c 1.268 from 2021-04-06, lint wrongly warned that conversion
    163  * to _Bool might lose accuracy.  C99 6.3.1.2 defines a special conversion
    164  * rule from scalar to _Bool though by comparing the value to 0.
    165  */
    166 _Bool
    167 to_bool(long a, long b)
    168 {
    169 	/* seen in fp_lib.h, function wideRightShiftWithSticky */
    170 	return a | b;
    171 }
    172 
    173 /* ARGSUSED */
    174 const char *
    175 cover_build_plus_minus(const char *arr, double idx)
    176 {
    177 	if (idx > 0.0)
    178 		/* expect+2: error: operands of '+' have incompatible types 'pointer to const char' and 'double' [107] */
    179 		/* expect+1: error: function 'cover_build_plus_minus' expects to return value [214] */
    180 		return arr + idx;
    181 	return arr + (unsigned int)idx;
    182 }
    183 
    184 int
    185 non_constant_expression(void)
    186 {
    187 	/*
    188 	 * Even though this variable definition looks like a constant, it
    189 	 * does not fall within C's definition of an integer constant
    190 	 * expression.  Due to that, lint does not perform constant folding
    191 	 * on the expression built from this variable and thus doesn't know
    192 	 * that the conversion will always succeed.
    193 	 */
    194 	const int not_a_constant = 8;
    195 	/* expect+1: warning: conversion from 'unsigned long long' to 'int' may lose accuracy [132] */
    196 	return not_a_constant * 8ULL;
    197 }
    198 
    199 /*
    200  * PR 36668 notices that lint wrongly complains about the possible loss.
    201  *
    202  * The expression 'u8_t << 8' is guaranteed to fit into an 'u16_t', and its
    203  * lower 8 bits are guaranteed to be clear.  'u16_t | u8_t' is guaranteed to
    204  * fit into 'u16_t'.
    205  *
    206  * Since tree.c 1.444 from 2022-05-26, lint tracks simple bitwise and
    207  * arithmetic constraints across a single expression.
    208  */
    209 void
    210 be16dec(void)
    211 {
    212 	/*
    213 	 * Before tree.c 1.444 from 2022-05-26, lint complained that the
    214 	 * conversion from 'int' to 'unsigned short' may lose accuracy.
    215 	 */
    216 	u16 = (u16_t)u8 << 8 | u8;
    217 }
    218 
    219 /*
    220  * Since tree.c 1.434 from 2022-04-19, lint infers the possible values of
    221  * expressions of the form 'integer & constant', see can_represent.
    222  */
    223 void
    224 be32enc(void)
    225 {
    226 	u8 = u32 >> 24 & 0xff;
    227 	u8 = u32 >> 16 & 0xff;
    228 	u8 = u32 >> 8 & 0xff;
    229 	u8 = u32 & 0xff;
    230 }
    231 
    232 void
    233 test_ic_mult(void)
    234 {
    235 	/* expect+1: warning: conversion from 'int' to 'unsigned char' may lose accuracy [132] */
    236 	u8 = u8 * u8;
    237 	u16 = u8 * u8;
    238 	/* expect+1: warning: conversion from 'int' to 'unsigned short' may lose accuracy [132] */
    239 	u16 = u16 * u8;
    240 	u32 = u16 * u16;
    241 
    242 	u32 = u16 * 65537ULL;
    243 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132] */
    244 	u32 = u16 * 65538ULL;
    245 
    246 	u16 = 0 * u16;
    247 	u16 = 1 * u16;
    248 	/* expect+1: warning: conversion from 'int' to 'unsigned short' may lose accuracy [132] */
    249 	u16 = 2 * u16;
    250 
    251 	// from __BITS, __SHIFTIN, __SHIFTOUT
    252 	u32 = (u16 & 1023ULL) / 1ULL * 1024ULL | (u16 & 1023ULL) / 1ULL * 1ULL;
    253 }
    254 
    255 void
    256 test_ic_div(void)
    257 {
    258 	// FIXME
    259 	/* expect+1: warning: conversion from 'int' to 'unsigned char' may lose accuracy [132] */
    260 	u8 = u8 / u8;
    261 	// FIXME
    262 	/* expect+1: warning: conversion from 'int' to 'unsigned char' may lose accuracy [132] */
    263 	u8 = u16 / u8;
    264 	// FIXME
    265 	/* expect+1: warning: conversion from 'int' to 'unsigned short' may lose accuracy [132] */
    266 	u16 = u8 / u8;
    267 	u16 = u32 / 65536;
    268 	/* expect+1: warning: conversion from 'unsigned int' to 'unsigned short' may lose accuracy [132] */
    269 	u16 = u32 / 65535;
    270 }
    271 
    272 void
    273 test_ic_mod(void)
    274 {
    275 	/* The result is between 0 and 254. */
    276 	u8 = u64 % u8;
    277 
    278 	/* The result is between 0 and 255. */
    279 	u8 = u64 % 256;
    280 
    281 	/* The result is between 0 and 256. */
    282 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned char' may lose accuracy [132] */
    283 	u8 = u64 % 257;
    284 
    285 	/* The result is between 0 and 1000. */
    286 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned char' may lose accuracy [132] */
    287 	u8 = u64 % 1000;
    288 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned int:9' may lose accuracy [132] */
    289 	bits.u9 = u64 % 1000;
    290 	bits.u10 = u64 % 1000;
    291 	u16 = u64 % 1000;
    292 
    293 	s8 = s16 % s8;
    294 	/* expect+1: warning: conversion from 'int' to 'signed char' may lose accuracy [132] */
    295 	s8 = s16 % s16;
    296 	s8 = s64 % 1;
    297 	s8 = s64 % (s16 & 1);
    298 	/* expect+1: warning: conversion from 'long long' to 'signed char' may lose accuracy [132] */
    299 	s8 = s64 % (s16 & 0);
    300 	s8 = (s64 & 0x7f) % s64;
    301 	/* expect+1: warning: conversion from 'long long' to 'signed char' may lose accuracy [132] */
    302 	s8 = (s64 & 0xff) % s64;
    303 }
    304 
    305 void
    306 test_ic_plus(void)
    307 {
    308 	/* expect+1: warning: conversion from 'long long' to 'signed char' may lose accuracy [132] */
    309 	s8 = -129 + s64 % 1;
    310 	/* expect+1: warning: conversion from 'long long' to 'signed char' may lose accuracy [132] */
    311 	s8 = -128 + s64 % 1;
    312 	/* expect+1: warning: conversion from 'long long' to 'signed char' may lose accuracy [132] */
    313 	s8 = 127 + s64 % 1;
    314 	/* expect+1: warning: conversion from 'long long' to 'signed char' may lose accuracy [132] */
    315 	s8 = 128 + s64 % 1;
    316 
    317 	/* expect+2: warning: conversion of negative constant -129 to unsigned type 'unsigned long long' [222] */
    318 	/* expect+1: warning: conversion from 'unsigned long long' to 'signed char' may lose accuracy [132] */
    319 	s8 = -129 + u64 % 1;
    320 	/* expect+2: warning: conversion of negative constant -128 to unsigned type 'unsigned long long' [222] */
    321 	/* expect+1: warning: conversion from 'unsigned long long' to 'signed char' may lose accuracy [132] */
    322 	s8 = -128 + u64 % 1;
    323 	/* expect+1: warning: conversion from 'unsigned long long' to 'signed char' may lose accuracy [132] */
    324 	s8 = 127 + u64 % 1;
    325 	/* expect+1: warning: conversion from 'unsigned long long' to 'signed char' may lose accuracy [132] */
    326 	s8 = 128 + u64 % 1;
    327 
    328 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned char' may lose accuracy [132] */
    329 	u8 = 0 + u64 % 1;
    330 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned char' may lose accuracy [132] */
    331 	u8 = 255 + u64 % 1;
    332 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned char' may lose accuracy [132] */
    333 	u8 = 256 + u64 % 1;
    334 
    335 	/* expect+1: warning: conversion from 'long long' to 'signed char' may lose accuracy [132] */
    336 	s8 = '0' + s64 % 10;
    337 }
    338 
    339 void
    340 test_ic_shl(void)
    341 {
    342 	u64 = u64 << u64;
    343 	s64 = s64 << s64;
    344 
    345 	u16 = u8 << 8;
    346 	/* expect+1: warning: conversion from 'int' to 'unsigned short' may lose accuracy [132] */
    347 	u16 = u8 << 9;
    348 	u32 = u16 << 16;
    349 	// XXX: missing warning as UINT has the same rank as INT, see portable_rank_cmp.
    350 	u32 = u16 << 17;
    351 	/* expect+1: warning: shift amount 56 is greater than bit-size 32 of 'int' [122] */
    352 	u64 = u8 << 56;
    353 	u64 = (u64_t)u8 << 56;
    354 	// XXX: missing warning, as the operand types of '=' are the same, thus no conversion.
    355 	u64 = (u64_t)u8 << 57;
    356 	/* expect+1: warning: shift amount 48 is greater than bit-size 32 of 'int' [122] */
    357 	u64 = u16 << 48;
    358 	u64 = (u64_t)u16 << 48;
    359 	// XXX: missing warning, as the operand types of '=' are the same, thus no conversion.
    360 	u64 = (u64_t)u16 << 49;
    361 	/* expect+1: warning: shift amount 32 equals bit-size of 'unsigned int' [267] */
    362 	u64 = u32 << 32;
    363 	u64 = (u64_t)u32 << 32;
    364 	// XXX: missing warning, as the operand types of '=' are the same, thus no conversion.
    365 	u64 = (u64_t)u32 << 33;
    366 }
    367 
    368 void
    369 test_ic_shr(void)
    370 {
    371 	u64 = u64 >> u64;
    372 	s64 = s64 >> s64;
    373 
    374 	u32 = u64 >> 32;
    375 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132] */
    376 	u32 = u64 >> 31;
    377 	u16 = u64 >> 48;
    378 	u16 = u32 >> 16;
    379 	u8 = u64 >> 56;
    380 	u8 = u32 >> 24;
    381 	u8 = u16 >> 8;
    382 
    383 	/*
    384 	 * No matter whether the big integer is signed or unsigned, the
    385 	 * result of '&' is guaranteed to be an unsigned value.
    386 	 */
    387 	u8 = (s64 & 0xf0) >> 4;
    388 	u8 = (s8 & 0xf0) >> 4;
    389 }
    390 
    391 void
    392 test_ic_bitand(void)
    393 {
    394 	u8 = u8 & u16;
    395 
    396 	/* expect+1: warning: conversion from 'unsigned int' to 'unsigned char' may lose accuracy [132] */
    397 	u8 = u16 & u32;
    398 }
    399 
    400 void
    401 test_ic_bitor(void)
    402 {
    403 	/* expect+1: warning: conversion from 'int' to 'unsigned char' may lose accuracy [132] */
    404 	u8 = u8 | u16;
    405 	u16 = u8 | u16;
    406 	/* expect+1: warning: conversion from 'unsigned int' to 'unsigned short' may lose accuracy [132] */
    407 	u16 = u8 | u32;
    408 	u32 = u8 | u32;
    409 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132] */
    410 	u32 = u8 | u64;
    411 	u64 = u8 | u64;
    412 }
    413 
    414 void
    415 test_ic_quest_colon(char c1, char c2)
    416 {
    417 	/* Both operands are representable as char. */
    418 	ch = cond ? '?' : ':';
    419 
    420 	/*
    421 	 * Both operands are representable as char. Clang-Tidy 17 wrongly
    422 	 * warns about a narrowing conversion from 'int' to signed type
    423 	 * 'char'.
    424 	 */
    425 	ch = cond ? c1 : c2;
    426 
    427 	/*
    428 	 * Mixing s8 and u8 results in a number from -128 to 255, which neither
    429 	 * fits in s8 nor u8.
    430 	 */
    431 	/* expect+1: warning: conversion from 'int' to 'signed char' may lose accuracy [132] */
    432 	s8 = cond ? s8 : u8;
    433 	/* expect+1: warning: conversion from 'int' to 'unsigned char' may lose accuracy [132] */
    434 	u8 = cond ? s8 : u8;
    435 }
    436 
    437 void
    438 test_ic_con(void)
    439 {
    440 	/* expect+1: warning: assignment of negative constant -1 to unsigned type 'unsigned char' [164] */
    441 	u8 = -1;
    442 	u8 = 0;
    443 	u8 = 255;
    444 	/* expect+1: warning: constant truncated by assignment [165] */
    445 	u8 = 256;
    446 
    447 	/* expect+1: warning: conversion of 'int' to 'signed char' is out of range [119] */
    448 	s8 = -129;
    449 	s8 = -128;
    450 	s8 = 127;
    451 	/* expect+1: warning: conversion of 'int' to 'signed char' is out of range [119] */
    452 	s8 = 128;
    453 }
    454 
    455 void
    456 test_ic_cvt(void)
    457 {
    458 	u16 = (u32 & 0x0000ff00);
    459 	u16 = (u32_t)(u32 & 0x0000ff00);
    460 	u16 = (u16_t)u32;
    461 	u16 = (u8_t)(u32 & 0xffff) << 8;
    462 }
    463 
    464 unsigned char
    465 test_bit_fields(unsigned long long m)
    466 {
    467 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned int:3' may lose accuracy [132] */
    468 	bits.u3 = bits.u32 & m;
    469 
    470 	bits.u5 = bits.u3 & m;
    471 	bits.u32 = bits.u5 & m;
    472 
    473 	/* expect+1: warning: conversion from 'unsigned long long' to 'unsigned char' may lose accuracy [132] */
    474 	return bits.u32 & m;
    475 }
    476 
    477 void
    478 compare_bit_field_to_integer_constant(void)
    479 {
    480 	static _Bool b;
    481 	static struct {
    482 		short s16:15;
    483 		unsigned short u16:15;
    484 		int s32:15;
    485 		unsigned u32:15;
    486 		long long s64:15;
    487 		unsigned long long u64:15;
    488 	} s;
    489 
    490 	// Since decl.c 1.180 from 2021-05-02 and before tree.c 1.624 from
    491 	// 2024-03-12, lint warned about a possible loss of accuracy [132]
    492 	// when promoting an 'unsigned long long' bit-field to 'int'.
    493 	b = s.s16 == 0;
    494 	b = s.u16 == 0;
    495 	b = s.s32 == 0;
    496 	b = s.u32 == 0;
    497 	b = s.s64 == 0;
    498 	b = s.u64 == 0;
    499 	b = !b;
    500 }
    501 
    502 /*
    503  * Before tree.c 1.626 from 2024-03-26, the usual arithmetic conversions for
    504  * bit-field types with the same base type but different widths simply took
    505  * the type of the left operand, leading to wrong warnings about loss of
    506  * accuracy when the right operand was wider than the left operand.
    507  */
    508 void
    509 binary_operators_on_bit_fields(void)
    510 {
    511 	struct {
    512 		u64_t u15:15;
    513 		u64_t u48:48;
    514 		u64_t u64;
    515 	} s = { 0, 0, 0 };
    516 
    517 	u64 = s.u15 | s.u48;
    518 	u64 = s.u48 | s.u15;
    519 	u64 = s.u15 | s.u48 | s.u64;
    520 	u64 = s.u64 | s.u48 | s.u15;
    521 	cond = (s.u15 | s.u48 | s.u64) != 0;
    522 	cond = (s.u64 | s.u48 | s.u15) != 0;
    523 
    524 	// Before tree.c from 1.638 from 2024-05-01, lint wrongly warned:
    525 	// warning: conversion of 'int' to 'int:4' is out of range [119]
    526 	s32 = 8 - bits.u3;
    527 }
    528 
    529 unsigned char
    530 combine_arithmetic_and_bit_operations(void)
    531 {
    532 	return 0xc0 | (u32 & 0x07c0) / 64;
    533 }
    534