Debian Codegraph @

llvm-project/clang-tools-extra/docs/clang-tidy/checks/bugprone/implicit-widening-of-multiplication-result.rst

raw
1 .. title:: clang-tidy - bugprone-implicit-widening-of-multiplication-result
2
3 bugprone-implicit-widening-of-multiplication-result
4 ===================================================
5
6 The check diagnoses instances where a result of a multiplication is implicitly
7 widened, and suggests (with fix-it) to either silence the code by making
8 widening explicit, or to perform the multiplication in a wider type,
9 to avoid the widening afterwards.
10
11 This is mainly useful when operating on very large buffers.
12 For example, consider:
13
14 .. code-block:: c++
15
16 void zeroinit(char* base, unsigned width, unsigned height) {
17 for(unsigned row = 0; row != height; ++row) {
18 for(unsigned col = 0; col != width; ++col) {
19 char* ptr = base + row * width + col;
20 *ptr = 0;
21 }
22 }
23 }
24
25 This is fine in general, but if ``width * height`` overflows,
26 you end up wrapping back to the beginning of ``base``
27 instead of processing the entire requested buffer.
28
29 Indeed, this only matters for pretty large buffers (4GB+),
30 but that can happen very easily for example in image processing,
31 where for that to happen you "only" need a ~269MPix image.
32
33
34 Options
35 -------
36
37 .. option:: UseCXXStaticCastsInCppSources
38
39 When suggesting fix-its for C++ code, should C++-style ``static_cast<>()``'s
40 be suggested, or C-style casts. Defaults to ``true``.
41
42 .. option:: UseCXXHeadersInCppSources
43
44 When suggesting to include the appropriate header in C++ code,
45 should ``<cstddef>`` header be suggested, or ``<stddef.h>``.
46 Defaults to ``true``.
47
48
49 Examples:
50
51 .. code-block:: c++
52
53 long mul(int a, int b) {
54 return a * b; // warning: performing an implicit widening conversion to type 'long' of a multiplication performed in type 'int'
55 }
56
57 char* ptr_add(char *base, int a, int b) {
58 return base + a * b; // warning: result of multiplication in type 'int' is used as a pointer offset after an implicit widening conversion to type 'ssize_t'
59 }
60
61 char ptr_subscript(char *base, int a, int b) {
62 return base[a * b]; // warning: result of multiplication in type 'int' is used as a pointer offset after an implicit widening conversion to type 'ssize_t'
63 }