V598. The 'memset/memcpy' function is used to nullify/copy the fields of 'Foo' class. Virtual table pointer will be damaged by this.

The analyzer has detected that such low-level functions as memset() or memcpy() are used to handle a class. It is inadmissible when a class has pointer to a virtual method table. The memset()/memcpy() functions might rewrite virtual table pointer (VPTR), and the program behavior will become undefined.

Consider the following code sample.

class MyClass
  int A, B, C;
  char buf[100];
  virtual ~MyClass() {}

  memset(this, 0, sizeof(*this));

Note that there is a virtual destructor in the class. It means that the class has a virtual table pointer. The programmer was too lazy to clear the class members separately and used the memset() function for that purpose. It will spoil the VPTR, since the memset() function does not know anything about it.

This is the correct code:

MyClass:: MyClass() : A(0), B(0), C(0)
  memset(buf, 0, sizeof(buf));

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

You can look at examples of errors detected by the V598 diagnostic.

Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++, and C#

goto PVS-Studio;
We use cookies for the analysis of events to improve our content and make user interaction more convenient. By continuing the view of our web-pages you accept the terms of using these files. You can find out more about cookie-files and privacy policy or close the notification, by clicking on the button. Learn More →
Do not show