1 .. title:: clang-tidy - bugprone-implicit-widening-of-multiplication-result
3 bugprone-implicit-widening-of-multiplication-result
4 ===================================================
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.
11 This is mainly useful when operating on very large buffers.
12 For example, consider:
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;
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.
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.
37 .. option:: UseCXXStaticCastsInCppSources
39 When suggesting fix-its for C++ code, should C++-style ``static_cast<>()``'s
40 be suggested, or C-style casts. Defaults to ``true``.
42 .. option:: UseCXXHeadersInCppSources
44 When suggesting to include the appropriate header in C++ code,
45 should ``<cstddef>`` header be suggested, or ``<stddef.h>``.
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'
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'
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'