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