1 /* $NetBSD: msg_324.c,v 1.7 2022/06/22 19:23:18 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 */ 18 19 void 20 example(char c, int i, unsigned u) 21 { 22 long long ll; 23 unsigned long long ull; 24 25 /* expect+1: warning: suggest cast from 'int' to 'long long' on op '+' to avoid overflow [324] */ 26 ll = c + i; 27 /* expect+1: warning: suggest cast from 'int' to 'long long' on op '-' to avoid overflow [324] */ 28 ll = i - c; 29 /* expect+1: warning: suggest cast from 'unsigned int' to 'unsigned long long' on op '*' to avoid overflow [324] */ 30 ull = c * u; 31 /* expect+1: warning: suggest cast from 'unsigned int' to 'unsigned long long' on op '+' to avoid overflow [324] */ 32 ull = u + c; 33 /* expect+1: warning: suggest cast from 'unsigned int' to 'unsigned long long' on op '-' to avoid overflow [324] */ 34 ull = i - u; 35 /* expect+1: warning: suggest cast from 'unsigned int' to 'unsigned long long' on op '*' to avoid overflow [324] */ 36 ull = u * i; 37 /* expect+1: warning: suggest cast from 'int' to 'long long' on op '<<' to avoid overflow [324] */ 38 ll = i << c; 39 40 /* 41 * The operators SHR, DIV and MOD cannot produce an overflow, 42 * therefore no warning is necessary for them. 43 */ 44 ll = i >> c; 45 ull = u / c; 46 ull = u % c; 47 48 /* 49 * Assigning the result of an increment or decrement operator to a 50 * differently-sized type is no unusual that there is no need to warn 51 * about it. It's also more unlikely that there is an actual loss 52 * since this only happens for a single value of the old type, unlike 53 * "ull = u * u", which has many more possibilities for overflowing. 54 */ 55 ull = u++; 56 ull = ++u; 57 ull = u--; 58 ull = --u; 59 } 60