V782. It is pointless to compute the distance between the elements of different arrays.


The analyzer detected meaningless code computing the distance between the elements of different arrays. Consider the following example:

ptrdiff_t offset()
{
  char path[9] = "test.txt";
  char resources[9] = "resr.txt";
  return path - resources;
}

Subtracting the addresses of the two arrays allocated on the stack is pointless and very likely to be an error.

To discuss all the suspicious operations involving pointers to arrays, it is convenient to divide pointers into two imaginary groups:

  • Group 'A' includes non-shifted pointers to stack-allocated arrays as well as arrays allocated using 'new' or 'malloc()'.
  • Group 'B' includes shifted pointers to arrays allocated using 'new' or 'malloc' or pointers that the analyzer has no information about.

Based on this division, we get a table of operations on pointers to arrays evaluation of which makes no sense (Table 1).

Table 1 – Meaningless pointer operations.

Table 1 – Meaningless pointer operations.

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-469.


Bugs Found

Checked Projects
336
Collected Errors
12 743