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