[cfe-commits] [PATCH 11/15] Fix Casting

dag at cray.com dag at cray.com
Fri Jan 11 11:34:52 PST 2013


Dmitri Gribenko <gribozavr at gmail.com> writes:

>> Hmm...I think const_cast is useful because it's a red flag.  I don't
>> think we should be hiding that as it can indicate a design issue.
>
> That's right, const_cast is a red flag, but in these places the cast
> is always safe, since we are storing pointers to AST nodes (which are
> always allocated non-const) into a C struct with void* pointer just to
> pass the struct to C-land.

Here's the kind of thing I'm worrying about:

CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent,
                                CXTranslationUnit TU) {
  assert(A && Parent && TU && "Invalid arguments!");
  CXCursor C = { GetCursorKind(A), 0, { Parent, const_cast<Attr *>(A), TU } };
  return C;
}

In this case we're const_cast'ing a pointer to a const Attr.  The caller
has no idea that the data it sent as an argument can be changed under
it.  IMHO that is quite unsafe and should be corrected properly.

All of the changes in CXCursor.cpp are of the same flavor.

                                -David



More information about the cfe-commits mailing list