Examples of errors detected by the V572 diagnostic.


V572. It is odd that the object which was created using 'new' operator is immediately cast to another type.


wxWidgets

V572 It is odd that the object which was created using 'new' operator is immediately cast to another type. base appbase.cpp 812


class wxFontMapper : public wxFontMapperBase
{
  ...
}

wxFontMapper *wxConsoleAppTraitsBase::CreateFontMapper()
{
  return (wxFontMapper *)new wxFontMapperBase;
}

Similar errors can be found in some other places:

  • V572 It is odd that the object which was created using 'new' operator is immediately cast to another type. base fmapbase.cpp 428

Cocos2d-x

V572 It is odd that the object which was created using 'new' operator is immediately cast to another type. ccactiontiledgrid.cpp 322


struct Tile
{
  Vec2    position;
  Vec2    startPosition;
  Size    delta;
};

Tile* _tiles;

void ShuffleTiles::startWithTarget(Node *target)
{
  ....
  _tiles = (struct Tile *)new Tile[_tilesCount];  // <=
  Tile *tileArray = (Tile*) _tiles;               // <=
  ....
}

Similar errors can be found in some other places:

  • V572 It is odd that the object which was created using 'new' operator is immediately cast to another type. luabasicconversions.cpp 1301

Tizen

V572 It is odd that the object which was created using 'new' operator is immediately cast to another type. scim_socket.cpp 136

V572 It is odd that the object which was created using 'new' operator is immediately cast to another type. scim_socket.cpp 140


struct sockaddr_un
{
  sa_family_t sun_family;
  char sun_path[108];
};

struct sockaddr_in
{
  sa_family_t sin_family;
  in_port_t sin_port;
  struct in_addr sin_addr;
  unsigned char sin_zero[sizeof (struct sockaddr) -
    (sizeof (unsigned short int)) -
    sizeof (in_port_t) -
    sizeof (struct in_addr)];
};

struct sockaddr
{
  sa_family_t sa_family;
  char sa_data[14];
};

class SocketAddress::SocketAddressImpl
{
  struct sockaddr *m_data;
  ....
  SocketAddressImpl (const SocketAddressImpl &other)
  {
    ....
    case SCIM_SOCKET_LOCAL:
        m_data = (struct sockaddr*) new struct sockaddr_un; // <=
        len = sizeof (sockaddr_un);
        break;
    case SCIM_SOCKET_INET:
        m_data = (struct sockaddr*) new struct sockaddr_in; // <=
        len = sizeof (sockaddr_in);
        break;
    ....
  }

  ~SocketAddressImpl () {
    if (m_data) delete m_data;                              // <=
  }
};

Structures of type sockaddr_un and sockaddr_in are created. However, they are stored and destroyed as sockaddr structures. All three types of the mentioned structures are not related among themselves. Three different structures have different sizes.

Similar errors can be found in some other places:

  • V572 It is odd that the object which was created using 'new' operator is immediately cast to another type. scim_socket.cpp 167
  • V572 It is odd that the object which was created using 'new' operator is immediately cast to another type. scim_socket.cpp 171


Do you make errors in the code?

Check your code
with PVS-Studio

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

goto PVS-Studio;