[cfe-dev] Proposal: -Wshadow-field flag

Ehsan Akhgari ehsan.akhgari at gmail.com
Sat Apr 18 11:05:05 PDT 2015


Hi everyone,

I would like to propose adding a -Wshadow-field to clang.  This flag would
emit a warning if a derived class declares a member with the same name as
one in one of the base classes, regardless of visibility.

Here is the rationale: In the Mozilla code base, there are fields that are
often declared with the same name in thousands of classes, either by
convention or through macros.  One concrete example is mRefCnt, which is
the variable holding the object's reference count.  Every once in a while,
we find bugs that are caused by the derived class inadvertently shadowing
the base member, and therefore code in the derived class and its
descendants in the hierarchy will access the wrong variable.  In the case
of mRefCnt, for example, these bugs are security sensitive since this
typically causes the reference count of the object to get out of balance,
causing use-after-free issues.  One can conceive of other similar problems
with other fields as well.

I was going to implement this warning in the clang plugin that we use for
static analysis of our code base, but realized that this may probably be
useful for other projects as well.  Is there any interest in this feature?
If yes, I would be happy to submit a patch.

Cheers,
-- 
Ehsan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150418/3bffd7d4/attachment.html>


More information about the cfe-dev mailing list