brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.4 KiB · efa7792 Raw
70 lines · plain
1.. title:: clang-tidy - bugprone-implicit-widening-of-multiplication-result2 3bugprone-implicit-widening-of-multiplication-result4===================================================5 6The check diagnoses instances where a result of a multiplication is implicitly7widened, and suggests (with fix-it) to either silence the code by making8widening explicit, or to perform the multiplication in a wider type,9to avoid the widening afterwards.10 11This is mainly useful when operating on very large buffers.12For 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 25This is fine in general, but if ``width * height`` overflows,26you end up wrapping back to the beginning of ``base``27instead of processing the entire requested buffer.28 29Indeed, this only matters for pretty large buffers (4GB+),30but that can happen very easily for example in image processing,31where for that to happen you "only" need a ~269MPix image.32 33 34Options35-------36 37.. option:: UseCXXStaticCastsInCppSources38 39   When suggesting fix-its for C++ code, should C++-style ``static_cast<>()``'s40   be suggested, or C-style casts. Defaults to `true`.41 42.. option:: UseCXXHeadersInCppSources43 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.. option:: IgnoreConstantIntExpr49 50   If the multiplication operands are compile-time constants (like literals or51   are ``constexpr``) and fit within the source expression type, do not emit a52   diagnostic or suggested fix.  Only considers expressions where the source53   expression is a signed integer type.  Defaults to `false`.54 55Examples:56 57.. code-block:: c++58 59  long mul(int a, int b) {60    return a * b; // warning: performing an implicit widening conversion to type 'long' of a multiplication performed in type 'int'61  }62 63  char* ptr_add(char *base, int a, int b) {64    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'65  }66 67  char ptr_subscript(char *base, int a, int b) {68    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'69  }70