# V1113. Potential resource leak. Calling the 'memset' function will change the pointer itself, not the allocated resource. Check the first and third arguments.

The analyzer has detected suspicious code. The address of the pointer referring to the dynamically allocated memory is passed to the 'memset' function. Such code can cause a memory leak after using the 'memset' function.

Take a look at the following case. Let's assume that this correctly working code existed in a project:

    void foo()
    {
      constexpr size_t count = ....;
      char array[count];

      memset(&array, 0, sizeof(array));
      ....
    }

An array is created on a stack and then its contents are zeroed using the 'memset' function. The original example has no errors: the array address is passed as the first argument, and the third argument is the actual size of the array in bytes.

A little later, for some reason, the programmer changed the buffer allocation from the stack to the heap:

    void foo()
    {
      constexpr size_t count = ....;
      char *array = (char*) malloc(count * sizeof(char));
      ....
      memset(&array, 0, sizeof(array));     // <=
      ....
    }

However, they did not change the 'memset' function call. This means that the address of the pointer on the function stack is now passed as the first argument, and the third argument is its size. This results in a memory leak and zeroing the pointer instead of the array contents.

Here is the fixed code:

    void PointerFixed()
    {
      ....
      constexpr size_t count = ....;
      char *array = (char*) malloc(count * sizeof(char));
      ....
      memset(array, 0, count * sizeof(char));
      ....
    }

Now the address of the memory segment on the heap is passed as the first argument, and the third argument is its size.