Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
class B {
    ...
};
B *getMeSomeSOrtOfBgetMeSomeSortOfB();
...
class D; // incomplete declaration
...
B *bp = getMeSomeSortOfB();
D *dp = (D *)bp; // old-stlye cast: legal, but inadvisable
dp = reinterpret_cast<D *>(bp); // new-style cast: legal, but inadvisable

Both an old-stlye style cast and a reinterpret_cast may be used to cast the valuea pointer to an incomplete class. However, the cast may result in a bad address.

Code Block
bgColor#FFcccc

class D : public SomeClass, public B {
    ...
};

B *getMeSomeSortOfB() { return new D; }

In the case shown above, it is likely that a correct cast of a B * to a D * would have to adjust the address by a fixed amount. However, at the point the cast is translated by the compiler the required information is not available and the address adjustment will not take place.

In the case of an old-style cast, the address adjustment will, however, take place if the cast is performed at a point where the structure of the class D is known. This different, context-dependent behavior of the old-style cast can result in very challenging bugs.

Risk Assessment

XXX

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

ERR01-A

1 (low)

2 (probable)

2 (high)

P4

L3

...