1 /* $NetBSD: msg_324.c,v 1.10 2024/01/28 08:17:27 rillig Exp $ */ 2 # 3 "msg_324.c" 3 4 // Test for message: suggest cast from '%s' to '%s' on op '%s' to avoid overflow [324] 5 6 /* 7 * This warning applies to binary operators if the result of the operator 8 * is converted to a type that is bigger than the operands' result type 9 * after the usual arithmetic promotions. 10 * 11 * In such a case, the operator's result would be truncated to the operator's 12 * result type (invoking undefined behavior for signed integers), and that 13 * truncated value would then be converted. At that point, a few bits may 14 * have been lost. 15 */ 16 17 /* lint1-flags: -g -S -w -P -X 351 */ 18 19 void 20 example(char c, int i, unsigned u) 21 { 22 long long ll; 23 unsigned long long ull; 24 25 /* expect+2: warning: suggest cast from 'int' to 'long long' on op '+' to avoid overflow [324] */ 26 /* expect+1: warning: 'll' set but not used in function 'example' [191] */ 27 ll = c + i; 28 /* expect+1: warning: suggest cast from 'int' to 'long long' on op '-' to avoid overflow [324] */ 29 ll = i - c; 30 /* expect+2: warning: suggest cast from 'unsigned int' to 'unsigned long long' on op '*' to avoid overflow [324] */ 31 /* expect+1: warning: 'ull' set but not used in function 'example' [191] */ 32 ull = c * u; 33 /* expect+1: warning: suggest cast from 'unsigned int' to 'unsigned long long' on op '+' to avoid overflow [324] */ 34 ull = u + c; 35 /* expect+1: warning: suggest cast from 'unsigned int' to 'unsigned long long' on op '-' to avoid overflow [324] */ 36 ull = i - u; 37 /* expect+1: warning: suggest cast from 'unsigned int' to 'unsigned long long' on op '*' to avoid overflow [324] */ 38 ull = u * i; 39 /* expect+1: warning: suggest cast from 'int' to 'long long' on op '<<' to avoid overflow [324] */ 40 ll = i << c; 41 42 /* 43 * The operators SHR, DIV and MOD cannot produce an overflow, 44 * therefore no warning is necessary for them. 45 */ 46 ll = i >> c; 47 ull = u / c; 48 ull = u % c; 49 50 /* 51 * Assigning the result of an increment or decrement operator to a 52 * differently-sized type is no unusual that there is no need to warn 53 * about it. It's also more unlikely that there is an actual loss 54 * since this only happens for a single value of the old type, unlike 55 * "ull = u * u", which has many more possibilities for overflowing. 56 */ 57 ull = u++; 58 ull = ++u; 59 ull = u--; 60 ull = --u; 61 } 62